Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drawing arrow using CGMutablePath. Based on: https://gist.github.com/mwermuth/07825df27ea28f5fc89a
extension CGMutablePath {
func addArrow(to point: CGPoint, tailWidth: CGFloat = 4.0, headWidth: CGFloat = 10.0, headLength: CGFloat = 14.0) {
let length = hypot(point.x - currentPoint.x, point.y - currentPoint.y)
let tailLength = length - headLength
let points: [CGPoint] = [
CGPoint(x: 0.0, y: tailWidth / 2),
CGPoint(x: tailLength, y: tailWidth / 2),
CGPoint(x: tailLength, y: headWidth / 2),
CGPoint(x: length, y: 0.0),
CGPoint(x: tailLength, y: -headWidth / 2),
CGPoint(x: tailLength, y: -tailWidth / 2),
CGPoint(x: 0.0, y: -tailWidth / 2)
]
let cosine: CGFloat = (point.x - currentPoint.x) / length
let sine: CGFloat = (point.y - currentPoint.y) / length
let transform = CGAffineTransform(a: cosine, b: sine, c: -sine, d: cosine, tx: currentPoint.x, ty: currentPoint.y)
addLines(between: points, transform: transform)
}
}
@niczyja

This comment has been minimized.

Copy link
Owner Author

@niczyja niczyja commented May 4, 2021

Usage:

let path = CGMutablePath()
path.move(to: start)
path.addArrow(to: end)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment