自定义view 同心圆

1.创建一个新的ios object-c 类 HypnosisView ,继承UIView。

HypnosisView.h

#import <Foundation/Foundation.h>


@interface HypnosisView : UIView
{
    
}

@end


2.实现drawRect 方法。

#import "HypnosisView.h"


@implementation HypnosisView

- (void)drawRect:(CGRect)rect
{
    // What rectangle am I filling?
    CGRect bounds = [self bounds];
    
    // 中心点
    CGPoint center;
    center.x = bounds.origin.x + bounds.size.width / 2.0;
    center.y = bounds.origin.y + bounds.size.height / 2.0;
    
    // 中心点到线的距离
    float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
    
    // 获取上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 10宽度画线
    CGContextSetLineWidth(context, 10);
    
    // 线颜色
    [[UIColor lightGrayColor] setStroke];

    // 循环画园
    for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20)
    {
        CGContextAddArc(context, center.x, center.y,
                        currentRadius, 0.0, M_PI * 2.0, YES);
        CGContextStrokePath(context);
    }
    
    // Create a string
    NSString *text = @"You are getting sleepy.";
    
    // 获取font对象
    UIFont *font = [UIFont boldSystemFontOfSize:28];
    
    // 设置矩形
    CGRect textRect;
    textRect.size = [text sizeWithFont:font];
    textRect.origin.x = center.x - textRect.size.width / 2.0;
    textRect.origin.y = center.y - textRect.size.height / 2.0;
    
    // 设置当前上下文颜色为黑色
    [[UIColor blackColor] setFill];
    
    //设置字体阴影
    CGSize offset = CGSizeMake(4, 3);
    CGColorRef color = [[UIColor darkGrayColor] CGColor];
    CGContextSetShadowWithColor(context, offset, 2.0, color);
    
    // 字符串画到textRect里
    [text drawInRect:textRect
            withFont:font];
}

@end


3.应用HypnosisView.

HypnosisterAppDelegate.h

#import <UIKit/UIKit.h>

// This is a forward declaration
@class HypnosisView;

@interface HypnosisterAppDelegate : NSObject
    <UIApplicationDelegate, UIScrollViewDelegate> {
    
    HypnosisView *view;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end


HypnosisterAppDelegate.m

#import "HypnosisterAppDelegate.h"
#import "HypnosisView.h"

@implementation HypnosisterAppDelegate

@synthesize window=_window;

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 获的窗口矩形
    CGRect wholeWindow = [[self window] bounds];
   

//创建UIScrollView对象,添加到窗口里
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:wholeWindow];
    [[self window] addSubview:scrollView];
    
    // 创建矩形空间 ,长宽是窗口的两倍
    CGRect reallyBigRect;
    reallyBigRect.origin = CGPointZero;
    reallyBigRect.size.width = wholeWindow.size.width * 2.0;
    reallyBigRect.size.height = wholeWindow.size.height * 2.0;
    [scrollView setContentSize:reallyBigRect.size];
    
    // 设置scrollview里面的view的初始位置
    CGPoint offset;
    offset.x = wholeWindow.size.width * 0.5;
    offset.y = wholeWindow.size.height * 0.5;
    [scrollView setContentOffset:offset];
    
    // 设置缩放
    [scrollView setMinimumZoomScale:0.5];
    [scrollView setMaximumZoomScale:5];
    [scrollView setDelegate:self];
    
    // 创建自定义view对象
    view = [[HypnosisView alloc] initWithFrame:reallyBigRect];
    [view setBackgroundColor:[UIColor clearColor]];
    [scrollView addSubview:view];
    
    [scrollView release];    
    //设置状态栏缓缓隐藏
    [[UIApplication sharedApplication] setStatusBarHidden:YES
                                    withAnimation:UIStatusBarAnimationFade];    
    [[self window] makeKeyAndVisible];
    return YES;
}

//因为委托对象为self ,所有缩放要实现此方法,返回缩放的View对象
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return view;
}

// A dealloc method that will never get called because
// HypnosisterAppDelegate will exist for the life of the application
- (void)dealloc
{
    [view release];
    [_window release];
    [super dealloc];
}

@end


完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值