《CAlayer——自定义layer》

来源:互联网 发布:vue.js数据绑定原理 编辑:程序博客网 时间:2024/06/07 01:02

一、自定义layer

以前想要在view中画东西,需要自定义view,创建一个类与之关联,让这个类继承自UIView,然后重写它的DrawRect:方法,然后在该方法中画图。

绘制图形的步骤:
(1)获取上下文
2)绘制图形
(3)渲染图形

  如果在layer上画东西,与上面的过程类似。

 1 //
 2 //  YYMylayer.m
 3 //  05-自定义layer(1)
 4 //
 5 //  Created by apple on 14-6-21.
 6 //  Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8 
 9 #import "YYMylayer.h"
10 
11 @implementation YYMylayer
12 //重写该方法,在该方法内绘制图形
13 -(void)drawInContext:(CGContextRef)ctx
14 {
15     //1.绘制图形
16     //画一个圆
17     CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
18     //设置属性(颜色)
19 //    [[UIColor yellowColor]set];
20     CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
21     
22     //2.渲染
23     CGContextFillPath(ctx);
24 }
25 @end

0 0