如何绘制方向箭头

时间:2018-02-05 15:22:08

标签: ios swift3 uibezierpath

我有许多方向和角度的线条,我使用UIBezierpath绘制它们。

我需要在线的一端画一个箭头;动态取决于给定的点。

修改: hand drawn but should give you a better understandin

编辑2:,Jake回答这里是我的代码

let y2 = line.point2Y

let path = UIBezierPath()

  path.move(to: CGPoint(x: x1, y: y1))
  path.addLine(to: CGPoint(x: x2, y: y2))
path.addArrow(start: CGPoint(x: x1, y: y1), end: CGPoint(x: x2, y: y2), pointerLineLength: ...
path.close()

let shape = CAShapeLayer()
let shapeBorder = CAShapeLayer()
shapeBorder.strokeColor = UIColor.black.cgColor
shapeBorder.lineJoin = kCALineJoinRound
shapeBorder.lineCap = kCALineCapRound
shapeBorder.lineWidth = 10
shapeBorder.addSublayer(shape)

shape.path = path.cgPath
shapeBorder.path = shape.path
shape.lineJoin = kCALineJoinRound
shape.lineCap = kCALineCapRound
shape.lineWidth = shapeBorder.lineWidth-5.0
shape.strokeColor = color

但是有一个阴影enter image description here

2 个答案:

答案 0 :(得分:3)

这对我有用:

extension UIBezierPath {
    func addArrow(start: CGPoint, end: CGPoint, pointerLineLength: CGFloat, arrowAngle: CGFloat) {
        self.move(to: start)
        self.addLine(to: end)

        let startEndAngle = atan((end.y - start.y) / (end.x - start.x)) + ((end.x - start.x) < 0 ? CGFloat(Double.pi) : 0)
        let arrowLine1 = CGPoint(x: end.x + pointerLineLength * cos(CGFloat(Double.pi) - startEndAngle + arrowAngle), y: end.y - pointerLineLength * sin(CGFloat(Double.pi) - startEndAngle + arrowAngle))
        let arrowLine2 = CGPoint(x: end.x + pointerLineLength * cos(CGFloat(Double.pi) - startEndAngle - arrowAngle), y: end.y - pointerLineLength * sin(CGFloat(Double.pi) - startEndAngle - arrowAngle))

        self.addLine(to: arrowLine1)
        self.move(to: end)
        self.addLine(to: arrowLine2)
    }
}

class MyViewController : UIViewController {
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        let arrow = UIBezierPath()
        arrow.addArrow(start: CGPoint(x: 200, y: 200), end: CGPoint(x: 50, y: 50), pointerLineLength: 30, arrowAngle: CGFloat(Double.pi / 4))

        let arrowLayer = CAShapeLayer()
        arrowLayer.strokeColor = UIColor.black.cgColor
        arrowLayer.lineWidth = 3
        arrowLayer.path = arrow.cgPath
        arrowLayer.fillColor = UIColor.clear.cgColor
        arrowLayer.lineJoin = kCALineJoinRound
        arrowLayer.lineCap = kCALineCapRound
        self.view.layer.addSublayer(arrowLayer)
    }
}

enter image description here

修改

另外,请务必将fillColor的{​​{1}}设置为CAShapeLayer。否则,您的图层将填充箭头的起点和末尾的一条线之间的区域。

答案 1 :(得分:1)

很好的例子,感谢您发布@Jake。小更新,使用 Xcode 12 应该是:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let arrow = UIBezierPath()
    arrow.addArrow(start: CGPoint(x: 200, y: 200), end: CGPoint(x: 50, y: 50), pointerLineLength: 30, arrowAngle: CGFloat(Double.pi / 4))

    let arrowLayer = CAShapeLayer()
    arrowLayer.strokeColor = UIColor.black.cgColor
    arrowLayer.lineWidth = 3
    arrowLayer.path = arrow.cgPath
    arrowLayer.fillColor = UIColor.clear.cgColor
    arrowLayer.lineJoin = CAShapeLayerLineJoin.round
    arrowLayer.lineCap = CAShapeLayerLineCap.round
    self.view.layer.addSublayer(arrowLayer)
}