iOS CAShapeLayer精講


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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章