绘制圆的一种简单方法是创建一个CAShapeLayer 并添加一个UIBezierPath。
objective-c
CAShapeLayer *circleLayer = [CAShapeLayer layer];
[circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)] CGPath]];
swift
let circleLayer = CAShapeLayer();
circleLayer.path = UIBezierPath(ovalIn: CGRect(x: 50, y: 50, width: 100, height: 100)).cgPath;
创建CAShapeLayer 后,我们将其path 设置为UIBezierPath。
我们的UIBezierPath 然后绘制bezierPathWithOvalInRect。我们设置的CGRect 会影响它的大小和位置。
现在我们有了自己的圈子,我们可以将其添加到我们的UIView 中作为sublayer。
objective-c
[[self.view layer] addSublayer:circleLayer];
swift
view.layer.addSublayer(circleLayer)
我们的圈子现在在我们的UIView 中可见。
如果我们希望自定义圈子的颜色属性,我们可以通过设置CAShapeLayer 的stroke- 和fill 颜色轻松实现。
objective-c
[circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
[circleLayer setFillColor:[[UIColor clearColor] CGColor]];
swift
shapeLayer.strokeColor = UIColor.red.cgColor;
shapeLayer.fillColor = UIColor.clear.cgColor;
更多属性可以在关于主题https://developer.apple.com/.../CAShapeLayer_class/index.html的文档中找到。