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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:47:36  来源:igfitidea点击:

An easy way to draw a circle using CAShapeLayer

iosobjective-cswiftuibezierpathcashapelayer

提问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 CAShapeLayerand add a UIBezierPath.

绘制圆的一种简单方法是创建一个CAShapeLayer并添加一个UIBezierPath.

objective-c

目标-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;

After creating the CAShapeLayerwe set its pathto be a UIBezierPath.

创建之后,CAShapeLayer我们将其path设置为 a UIBezierPath

Our UIBezierPaththen draws a bezierPathWithOvalInRect. The CGRectwe set will effect its size and position.

我们UIBezierPath然后绘制一个bezierPathWithOvalInRect. 在CGRect我们设置会影响它的大小和位置。

Now that we have our circle, we can add it to our UIViewas a sublayer.

现在我们有了我们的圈子,我们可以将它添加到我们UIViewsublayer.

objective-c

目标-c

[[self.view layer] addSublayer:circleLayer];

swift

迅速

view.layer.addSublayer(circleLayer)

Our circle is now visible in our UIView.

我们的圆圈现在在我们的UIView.

Circle

圆圈

If we wish to customise our circle's color properties we can easily do so by setting the CAShapeLayer's stroke- and fillcolor.

如果我们希望自定义圆的颜色属性,我们可以通过设置CAShapeLayer's stroke- 和fill颜色轻松实现。

objective-c

目标-c

[circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
[circleLayer setFillColor:[[UIColor clearColor] CGColor]];

swift

迅速

shapeLayer.strokeColor = UIColor.red.cgColor;
shapeLayer.fillColor = UIColor.clear.cgColor;

Circle_wColors

Circle_wColors

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