swift - 如何在 Swift 5.0 中通过 CAShapeLayer 以点开始和结束一行

标签 swift uibezierpath cashapelayer

如何以点开始和结束一行,如下图所示:

enter image description here

我可以管理画圆,但我不知道如何在行首和行尾添加点:

let arcCenter = CGPoint(x: vwCircle.frame.width / 2,
                        y: vwCircle.frame.height / 2)

let circlePath = UIBezierPath(arcCenter: arcCenter,
                                 radius: 15,
                             startAngle: 0,
                               endAngle: CGFloat(Double.pi),
                              clockwise: true)

shapeLayer = CAShapeLayer()
shapeLayer!.path = circlePath.cgPath

//change the fill color
shapeLayer!.fillColor = UIColor.clear.cgColor
//you can change the stroke color
shapeLayer!.strokeColor = UIColor.red.cgColor
//you can change the line width
shapeLayer!.lineWidth = 3.0

vwCircle.layer.addSublayer(shapeLayer!)

最佳答案

执行此操作的最简单方法(因此也是我执行此操作的方法)就是使用更多形状图层。对于每个点,创建一个形状图层,其形状是中心的一个点。然后添加该图层并对其进行旋转变换到您想要的角度,然后进行平移变换到圆的半径。

enter image description here

所以,该图实际上是三个形状层:外圆、一个点和另一个点。以下是顶部点的创建方式:

let angle : CGFloat = .pi * 2 * 3 / 4
// ...
let lay = CAShapeLayer()
lay.frame = circleLayer.bounds
lay.strokeColor = UIColor.black.cgColor
lay.fillColor = UIColor.black.cgColor
let dot = UIBezierPath(
    arcCenter: CGPoint(x: circleLayer.bounds.midX, y: circleLayer.bounds.midY), 
    radius: 3, startAngle: 0, endAngle: .pi*2.0, clockwise: true)
lay.path = dot.cgPath
circleLayer.addSublayer(lay)
// and now add the transform
lay.transform = CATransform3DMakeRotation(angle, 0, 0, 1)
lay.transform = CATransform3DTranslate(lay.transform, circleRadius, 0, 0)

通过更改值.pi * 2 * 3/4,您可以将点放置在圆上的任何位置。

关于swift - 如何在 Swift 5.0 中通过 CAShapeLayer 以点开始和结束一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59243082/

相关文章:

ios - 使用 RxSwift 从 UITableViewCell 发送回调到 UIViewController

ios - 如何在按下按钮时扫描二维码?

ios - 如何在 UITableView - Swift 中制作 list ?

ios - 当 View 位于 SwiftUI 中的 TabView 中时,NavigationView 标题不会出现

ios - UIBezierPath Init() 不期望 byRoundingCorners 参数

ios - 带有 UIBezierPath 的 CAShapeLayer

ios - 自定义 UIImageView 不工作

ios - UIView 图层上的阴影

ios - 将 CAShapeLayer 圆动画化为圆角三角形

ios - 按比例绘制圆圈以快速包含 View