CAShaperLayer和UIBezeierPath 共同作用

2016-08-24  本文已影响0人  我是一只不会飞的小小鸟

CAShaperlayer 的优点:

1.渲染快速。CAShapeLayer使用了硬件加速,绘制同一图形会比用Core Graphics快很多。

2.高效使用内存。一个CAShapeLayer不需要像普通CALayer一样创建一个寄宿图形,所以无论有多大,都不会占用太多的内存。

3.不会被图层边界剪裁掉。一个CAShapeLayer可以在边界之外绘制。你的图层路径不会像在使用Core Graphics的普通CALayer一样被剪裁掉(如我们在第二章所见)。

4.不会出现像素化。当你给CAShapeLayer做3D变换时,它不像一个有寄宿图的普通图层一样变得像素化。

用CAShaperLayer创建一个小火柴

#import "DrawingView.h"

#import

@interface ViewController ()

@property (nonatomic, weak) IBOutlet UIView *containerView;

@end

@implementation ViewController

- (void)viewDidLoad

{

[super viewDidLoad];

//create path

UIBezierPath *path = [[UIBezierPath alloc] init];

[path moveToPoint:CGPointMake(175, 100)];

[path addArcWithCenter:CGPointMake(150, 100) radius:25 startAngle:0 endAngle:2*M_PI 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)];

//create shape layer

CAShapeLayer *shapeLayer = [CAShapeLayer layer];

shapeLayer.strokeColor = [UIColor redColor].CGColor;

shapeLayer.fillColor = [UIColor clearColor].CGColor;

shapeLayer.lineWidth = 5;

shapeLayer.lineJoin = kCALineJoinRound;

shapeLayer.lineCap = kCALineCapRound;

shapeLayer.path = path.CGPath;

//add it to our view

[self.containerView.layer addSublayer:shapeLayer];

}

@end

上一篇下一篇

猜你喜欢

热点阅读