Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drawing an Arrow with Swift
extension UIBezierPath {
class func getAxisAlignedArrowPoints(inout points: Array<CGPoint>, forLength: CGFloat, tailWidth: CGFloat, headWidth: CGFloat, headLength: CGFloat ) {
let tailLength = forLength - headLength
points.append(CGPointMake(0, tailWidth/2))
points.append(CGPointMake(tailLength, tailWidth/2))
points.append(CGPointMake(tailLength, headWidth/2))
points.append(CGPointMake(forLength, 0))
points.append(CGPointMake(tailLength, -headWidth/2))
points.append(CGPointMake(tailLength, -tailWidth/2))
points.append(CGPointMake(0, -tailWidth/2))
}
class func transformForStartPoint(startPoint: CGPoint, endPoint: CGPoint, length: CGFloat) -> CGAffineTransform{
let cosine: CGFloat = (endPoint.x - startPoint.x)/length
let sine: CGFloat = (endPoint.y - startPoint.y)/length
return CGAffineTransformMake(cosine, sine, -sine, cosine, startPoint.x, startPoint.y)
}
class func bezierPathWithArrowFromPoint(startPoint:CGPoint, endPoint: CGPoint, tailWidth: CGFloat, headWidth: CGFloat, headLength: CGFloat) -> UIBezierPath {
let xdiff: Float = Float(endPoint.x) - Float(startPoint.x)
let ydiff: Float = Float(endPoint.y) - Float(startPoint.y)
let length = hypotf(xdiff, ydiff)
var points = [CGPoint]()
self.getAxisAlignedArrowPoints(&points, forLength: CGFloat(length), tailWidth: tailWidth, headWidth: headWidth, headLength: headLength)
var transform: CGAffineTransform = self.transformForStartPoint(startPoint, endPoint: endPoint, length: CGFloat(length))
var cgPath: CGMutablePathRef = CGPathCreateMutable()
CGPathAddLines(cgPath, &transform, points, 7)
CGPathCloseSubpath(cgPath)
var uiPath: UIBezierPath = UIBezierPath(CGPath: cgPath)
return uiPath
}
}
@mwermuth

This comment has been minimized.

Copy link
Owner Author

@mwermuth mwermuth commented Sep 11, 2014

Use it like this

let arrowPath = UIBezier. bezierPathWithArrowFromPoint(CGPointMake(0,0), CGPointMake(0,20), 4, 8, 6)
@boyxgc

This comment has been minimized.

Copy link

@boyxgc boyxgc commented Oct 3, 2014

Good job!

@arbel03

This comment has been minimized.

Copy link

@arbel03 arbel03 commented Apr 20, 2016

Perfect job 💯

@sivamurugan

This comment has been minimized.

Copy link

@sivamurugan sivamurugan commented Jun 22, 2016

Excellent "mwermuth" Awesome work ... Helps me lot ... Please let me know this to draw in the real time drawing from one point to another point ... thanks in advance.

@gsujathap

This comment has been minimized.

Copy link

@gsujathap gsujathap commented Jan 6, 2017

How to use it between views?

@lehuudungle

This comment has been minimized.

Copy link

@lehuudungle lehuudungle commented May 6, 2017

how to draw curved arrow ???

@Akash20395

This comment has been minimized.

Copy link

@Akash20395 Akash20395 commented Jan 12, 2018

let arrowPath = UIBezier. bezierPathWithArrowFromPoint(CGPointMake(0,0), CGPointMake(0,20), 4, 8, 6)

How overlay this arrowPath on UIImageView() ?

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