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