Skip to content

Instantly share code, notes, and snippets.

@schpaa
Last active August 29, 2015 14:06
Show Gist options
  • Save schpaa/700cadc19f90db824a85 to your computer and use it in GitHub Desktop.
Save schpaa/700cadc19f90db824a85 to your computer and use it in GitHub Desktop.
extension NSBezierPath {
var CGPath: CGMutablePath! {
get {
if self.elementCount == 0 {
return nil
}
let path = CGPathCreateMutable()
var didClosePath = false
for i in 0..<self.elementCount {
var points:[NSPoint] = [NSPoint](count: 3, repeatedValue: NSZeroPoint)
switch self.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 CGPathCreateMutableCopy(path)
}
}
}
//Now you can:
class TerrainLayer: CALayer {
override func drawInContext(ctx: CGContext!) {
let path = NSBezierPath(roundedRect: bounds, xRadius:10, yRadius:10).CGPath
CGPathAddRect(path, nil, bounds)
CGContextSetFillColorWithColor(ctx, NSColor.redColor().CGColor)
CGContextAddPath(ctx, path)
CGContextFillPath(ctx)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment