01_第6章_创建UIView子类


本章主要介绍了一些绘图操作和UIScrollView的基本操作(滚动和缩放)。


demo讲解:

1.创建一个催眠程序。新建工程Hypnosister,新建类HypnosisView,父类选择NSObject

2.drawRect方法。绘图的上下文类型是CGContextRef(Core Graphics Context Reference),负责合并绘图命令并生成一个图形,该图形就是视图的外观。

覆盖HypnosisView.m中的drawRect:方法


- (void)drawRect:(CGRect)rect
{
    //获取绘图区域
    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 = hypotf(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);
    }
    
    //创建字符串
    NSString *text = @"You are getting sleepy.";
    
    //获取绘图所需的字体
    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];
    
    //阴影设为向右偏移4点,向下偏移3点,暗灰色,2点模糊半径
    CGSize offset = CGSizeMake(4, 3);
    CGColorRef color = [[UIColor darkGrayColor] CGColor];
    CGContextSetShadowWithColor(context, offset, 2.0, color);
    
    //绘制字符串
    [text drawInRect:textRect withFont:font];
}


3.UIScrollView的使用:


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    //创建窗口大小的CGRect
    CGRect wholeWindow = [[self window] bounds];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:wholeWindow];
    [self.window addSubview:scrollView];
    
    //将视图大小设为窗口的2倍
    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对象里居中
    CGPoint offset;
    offset.x = wholeWindow.size.width / 2.0;
    offset.y = wholeWindow.size.height / 2.0;
    [scrollView setContentOffset:offset];
    
    //启用缩放功能
    [scrollView setMinimumZoomScale:0.5];
    [scrollView setMaximumZoomScale:5.0];
    [scrollView setDelegate:self];
    
    //创建HypnosisView实例,大小和窗口相同
    view = [[HypnosisView alloc] initWithFrame:reallyBigRect];
    
    //将该视图的背景色设为“clear”
    [view setBackgroundColor:[UIColor clearColor]];
    
    //将视图加入视图层次结构,使其能在窗口上显示
    [scrollView addSubview:view];
    
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    [self.window makeKeyAndVisible];
    return YES;
}

//UIScrollView代理
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return view;
}


tips:

1.在.h文件中声明类实例时,不必import该类头文件,可先声明@class SomeClass,然后在.m文件中再import该文件,这样可加快编译速度,在大型项目中有很重要的意义。



demo下载:  https://github.com/ZiliKou/Hypnosister



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值