ios - 从一点到另一点的弯曲箭头

标签 ios swift

我需要绘制一个从一点到另一点的弯曲箭头。我确实设法从下面的扩展中得到了一个直箭头。

extension UIBezierPath {

static func arrow(from start: CGPoint, to end: CGPoint, tailWidth: CGFloat, headWidth: CGFloat, headLength: CGFloat) -> UIBezierPath {
    let length = hypot(end.x - start.x, end.y - start.y)
    let tailLength = length - headLength

    func p(_ x: CGFloat, _ y: CGFloat) -> CGPoint { return CGPoint(x: x, y: y) }
    let points: [CGPoint] = [
        p(0, tailWidth / 2),
        p(tailLength, tailWidth / 2),
        p(tailLength, headWidth / 2),
        p(length, 0),
        p(tailLength, -headWidth / 2),
        p(tailLength, -tailWidth / 2),
        p(0, -tailWidth / 2)
    ]

    let cosine = (end.x - start.x) / length
    let sine = (end.y - start.y) / length
    let transform = CGAffineTransform(a: cosine, b: sine, c: -sine, d: cosine, tx: start.x, ty: start.y)

    let path = CGMutablePath()
    path.addLines(between: points, transform: transform)


    path.closeSubpath()

    return self.init(cgPath: path)
}}

我目前从上面的代码中得到了什么。 What I have now

我需要什么。

What I need

最佳答案

使用addArc方法创建曲线(而不是直线):-

path.addArc(tangent1End: start, tangent2End: end, radius: 30)

path.addArc(center: CGPoint(x: 100, y: 100), radius: 30, startAngle: 270, endAngle: 360, clockwise: true, transform: transform)

关于ios - 从一点到另一点的弯曲箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55038312/

相关文章:

ios - 使用 NSSortDescriptor 通过 setDateFomat :@"dd. MMMM-EEEE 对 UITableview 标题进行排序

ios - 编译 Storyboard LaunchScreen.storyboard

iphone - iOS 持久通知/快速启动

iphone - NSPredicate 调用测试方法从 Core Data 获取结果

ios - CollectionView fatal error : Index out of range

swift - 使用 UITextChecker 对 Swift 5 中的希伯来语文本进行拼写检查

ios - 在 GMSMapView 中添加和删除 KVO "myLocation"的观察者

swift - ui 文本字段更新 SKLabelNode

ios - 如何将网页另存为PDF?

ios - Swift - 自定义 Segue 错误( 'view' 的使用不明确)