CALayer的自定义视图和自定义图层的执行顺序 (图片翻转的技巧)

一、CALayer自定义视图-->自定图层的执行顺序
1>执行自定义视图的- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 方法
2>执行自定义视图的 - (void)drawRect:(CGRect)rect方法
3>执行自定义图层的 - (void)drawInContext:(CGContextRef)ctx方法
二、图片翻转的技巧
- (void)drawInContext:(CGContextRef)ctx
{
// 在对坐标系进行调整前,需要保存状态,绘图完毕后,需要恢复状态
CGContextSaveGState(ctx);
// 画头像
// 图片翻转的技巧
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -200);

UIImage *image = [UIImage imageNamed:@"头像1"];
CGContextDrawImage(ctx, CGRectMake(50, 50, 100, 100), image.CGImage);

CGContextRestoreGState(ctx);

// 画一个圆
CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 100, 100));
CGContextSetRGBFillColor(ctx, 0.0, 1.0, 1.0, 1.0);
CGContextDrawPath(ctx, kCGPathFill);

// 再画一个圆
CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 100, 100));
CGContextSetRGBFillColor(ctx, 0.0, 0.0, 1.0, 1.0);
CGContextDrawPath(ctx, kCGPathFill);
}

原文地址:https://www.cnblogs.com/yinqiang/p/3486798.html