Forked from jorgenisaksson/gist:76a8dae54fd3dc4e31c2
Last active
October 3, 2015 08:56
-
-
Save yageek/c90e8d0e29e813ff5561 to your computer and use it in GitHub Desktop.
Create a CGPath from an NSBezierPath in Swift. Great for CALayers for example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from Cocoa Drawing Guide's "Create a CGPathRef fram an NSBezierPath Object" | |
func CGPathFromNSBezierPath(nsPath: NSBezierPath) -> CGPath! { | |
if nsPath.elementCount == 0 { | |
return nil | |
} | |
let path = CGPathCreateMutable() | |
var didClosePath = false | |
for i in 0..nsPath.elementCount { | |
var points = NSPoint[](count: 3, repeatedValue: NSZeroPoint) | |
switch nsPath.elementAtIndex(i, associatedPoints: &points) { | |
case .MoveToBezierPathElement:CGPathMoveToPoint(path, nil, points[0].x, points[0].y) | |
case .LineToBezierPathElement:CGPathAddLineToPoint(path, nil, points[0].x, points[0].y) | |
case .CurveToBezierPathElement:CGPathAddCurveToPoint(path, nil, points[0].x, points[0].y, points[1].x, points[1].y, points[2].x, points[2].y) | |
case .ClosePathBezierPathElement:CGPathCloseSubpath(path) | |
didClosePath = true | |
} | |
} | |
if !didClosePath { | |
CGPathCloseSubpath(path) | |
} | |
return CGPathCreateCopy(path) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment