ios 使用 CAShapeLayer 绘制圆的简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28442516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An easy way to draw a circle using CAShapeLayer
提问by Aleksander Azizi
In questions like How to draw a smooth circle..., ...Draw Circle...and ...draw filled Circlesthe question and answer is very broad, contains lots of unnecessary steps and the methods used isn't always the easiest to re-create or manage.
在诸如How to draw a smooth circle...、...Draw Circle...和...draw Filled Circles之类的问题中,问题和答案非常广泛,包含许多不必要的步骤,并且使用的方法并不总是最容易重新创建或管理。
What is an easy way to draw a circle and add it to my UIView
?
什么是绘制圆并将其添加到我的简单方法UIView
?
回答by Aleksander Azizi
An easy way to draw a circle is to create a CAShapeLayer
and add a UIBezierPath
.
绘制圆的一种简单方法是创建一个CAShapeLayer
并添加一个UIBezierPath
.
CAShapeLayer *circleLayer = [CAShapeLayer layer];
[circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)] CGPath]];
let circleLayer = CAShapeLayer();
circleLayer.path = UIBezierPath(ovalIn: CGRect(x: 50, y: 50, width: 100, height: 100)).cgPath;
After creating the CAShapeLayer
we set its path
to be a UIBezierPath
.
创建之后,CAShapeLayer
我们将其path
设置为 a UIBezierPath
。
Our UIBezierPath
then draws a bezierPathWithOvalInRect
. The CGRect
we set will effect its size and position.
我们UIBezierPath
然后绘制一个bezierPathWithOvalInRect
. 在CGRect
我们设置会影响它的大小和位置。
Now that we have our circle, we can add it to our UIView
as a sublayer
.
现在我们有了我们的圈子,我们可以将它添加到我们UIView
的sublayer
.
[[self.view layer] addSublayer:circleLayer];
view.layer.addSublayer(circleLayer)
Our circle is now visible in our UIView
.
我们的圆圈现在在我们的UIView
.
If we wish to customise our circle's color properties we can easily do so by setting the CAShapeLayer
's stroke
- and fill
color.
如果我们希望自定义圆的颜色属性,我们可以通过设置CAShapeLayer
's stroke
- 和fill
颜色轻松实现。
[circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
[circleLayer setFillColor:[[UIColor clearColor] CGColor]];
shapeLayer.strokeColor = UIColor.red.cgColor;
shapeLayer.fillColor = UIColor.clear.cgColor;
Additionall properties can be found over at ?'s documentation on the subject https://developer.apple.com/.../CAShapeLayer_class/index.html.
可以在 ? 的文档中找到关于该主题的其他属性https://developer.apple.com/.../CAShapeLayer_class/index.html。
回答by azhman_adam
Using CAShapeLayer Class makes drawing easy... 1.Create CAShapeLayer Object 2.Create a Circular path 3.Set the path to the wanted CAShapeLayer path 4.Add the layer to your view
使用 CAShapeLayer 类使绘图变得容易... 1.创建 CAShapeLayer 对象 2.创建圆形路径 3.将路径设置为想要的 CAShapeLayer 路径 4.将图层添加到您的视图
let shapeLayer = CAShapeLayer()
let center = view.center
let circulPath = UIBezierPath(arcCenter: center, radius: 100, startAngle: 0, endAngle: 2.0 * CGFloat.pi, clockwise: true)
shapeLayer.path = circulPath.cgPath
view.layer.addSublayer(shapeLayer)
Note That ,here I draw the circle from center of the view. You can also set the fill color for your circle like bellow:
请注意,这里我从视图的中心绘制圆圈。您还可以为您的圆圈设置填充颜色,如下所示:
shapeLayer.fillColor = UIColor.red.cgColor
for further study you can check on CALayer.com
如需进一步研究,您可以查看 CALayer.com