标签:
一般我们使用CALayer的时候如何想要在CALayer上面绘图有两种方式:
1:新建一个类 继承自CALayer,然后重写他的一个方法:
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 }
然后新建该类的实例后 调用setNeedsDisplay即可以调用到drawInContext方法,
2:使用CALayer的委托方法,首先继承calayer的委托,实现他的一个方法:
36 -(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
37 {
38 //1.绘制图形
39 //画一个圆
40 CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
41 //设置属性(颜色)
42 // [[UIColor yellowColor]set];
43 CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
44
45 //2.渲染
46 CGContextFillPath(ctx);
47 }
然后设置layer.delegate = self后,自然会调用该方法的标签:
原文地址:http://www.cnblogs.com/mrzhu/p/5359150.html