CAShapeLayer
继承自CALayer
,因此,可使用CALayer
的所有属性。但是,CAShapeLayer
需要和贝塞尔曲线配合使用才有意义。
上面只是部分说明内容,由于较长,只放一部分出来。这里是说CAShapeLayer
是在其座标系统内绘制贝塞尔曲线的。因此,使用CAShapeLayer
需要与UIBezierPath
一起使用。
它有一个path
属性,而UIBezierPath
就是对CGPathRef
类型的封装,因此这两者配合起来使用才可以的哦!
@property(nullable) CGPathRef path;
CAShapeLayer与UIBezierPath画圆
CAShapeLayer *layer = [CAShapeLayer layer];
layer.bounds = CGRectMake(0, 0, 100, 100);
layer.position = CGPointMake(50, 50);
layer.fillColor = [UIColor clearColor].CGColor;
layer.lineWidth = 2.0;
layer.strokeColor = [UIColor redColor].CGColor;
#####首先确定layer在父layer中的位置#########
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 50, 50)];
注意bezierPathWithOvalInRect函数的参数:不管是画圆圈,还是画矩形,frame的位置是相对于CAShapeLayer来说的。
layer.path = circlePath.CGPath;
[self.view.layer addSublayer:layer];
参考
http://www.jianshu.com/p/5f08035056f6