#import <QuartzCore/QuartzCore.h>
// 实例化子图层
CALayer *myLayer = [CALayer layer];
[myLayer setBounds:CGRectMake(0, 0, 200, 200)];
[myLayer setBackgroundColor:[UIColor redColor].CGColor];
[myLayer setPosition:CGPointMake(100, 100)];
[self.view.layer addSublayer:myLayer];
// 提示:不能将视图设置为layer的代理
// [myLayer setDelegate:self.view];
[myLayer setDelegate:self];
// 提示,如果要重绘CALayer,必须要调用setNeedDisplay方法
[myLayer setNeedsDisplay];
NSLog(@"%@", myLayer);
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
CGRect rect = CGRectMake(50, 50, 100, 100);
CGContextSetRGBFillColor(ctx, 0.0, 0.0, 1.0, 1.0);
CGContextSetRGBStrokeColor(ctx, 0.0, 1.0, 0.0, 1.0);
CGContextAddRect(ctx, rect);
CGContextDrawPath(ctx, kCGPathFillStroke);