CoreAnimation----CAShapeLayer
先说说使用CAShapeLayer的优点: GPU执行, GPU执行, GPU执行
CAShapeLayer
1. CAShapeLayer的优点
- 渲染迅速,硬件加速GPU执行 。
- 内存利用率高,不需要寄宿图,所以比CALayer的内存占用还低。 。
- 图层边界不会被裁减掉,CAShapeLayer可在边界外进行绘制
- 不会 像素化
2. 使用CGPath
- CAShapeLayer可绘制所有通过CGPath表示的形状
- 这个形状不一定要闭合
- 可控制属性如:lineWidth(线宽),lineCap(线条结尾的样式),lineJoin(线条之间结合点的样式)
使用CAShapeLayer画“火柴人”:
- (void)viewDidLoad {
[super viewDidLoad];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(175, 100)];
[path addArcWithCenter:CGPointMake(150, 100) radius:25 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
[path moveToPoint:CGPointMake(150, 125)];
[path addLineToPoint:CGPointMake(150, 175)];
[path addLineToPoint:CGPointMake(125, 225)];
[path moveToPoint:CGPointMake(150, 175)];
[path addLineToPoint:CGPointMake(175, 225)];
[path moveToPoint:CGPointMake(100, 150)];
[path addLineToPoint:CGPointMake(200, 150)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = [UIColor redColor].CGColor;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.lineWidth = 5.0f;
shapeLayer.lineJoin = @"round";
shapeLayer.lineCap = @"round";
shapeLayer.path = path.CGPath;
[self.view.layer addSublayer:shapeLayer];
NSLog(@"-------viewDidlod-------");
}
3. 圆角
- 除了设置CALayer的cornerRadius,还可以单独设置一个角的圆角
设置矩形的三个角为圆角
- (void)viewDidLoad {
[super viewDidLoad];
CGRect tect = CGRectMake(50, 50, 100, 100);
CGSize radii = CGSizeMake(20, 20);
UIRectCorner corner = UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:tect byRoundingCorners:corner cornerRadii:radii];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = path.CGPath;
[self.view.layer addSublayer:shapeLayer];
NSLog(@"-------viewDidlod-------");
}