//DMLayer.m
-(void)drawInContext:(CGContextRef)ctx
{
CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
CGContextAddArc(ctx, 40, 40, 20, 0, M_PI * 2, 1);
CGContextStrokePath(ctx);
}
DMLayer *dlayer = [[DMLayer alloc] init];
dlayer.backgroundColor = [UIColor orangeColor].CGColor;
dlayer.anchorPoint = CGPointZero;
dlayer.frame = CGRectMake(10, 300, 100, 100);
//此句非常重要,需要手动调用,才能画出来
[dlayer setNeedsDisplay];
[self.view.layer addSublayer:dlayer];
//使用代理
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(50, 50, 100, 100);
layer.backgroundColor = [UIColor blueColor].CGColor;
// 设置代理,让代理帮图层画东西
layer.delegate = self;
// 只有调用这个方法才会进行第一次的绘制
[layer setNeedsDisplay];
[self.view.layer addSublayer:layer];
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 50, 50));
CGContextFillPath(ctx);
}