Skip to content

Instantly share code, notes, and snippets.

@anthonycastelli
Last active January 16, 2016 00:15
Show Gist options
  • Save anthonycastelli/9290698cc3dd397359a9 to your computer and use it in GitHub Desktop.
Save anthonycastelli/9290698cc3dd397359a9 to your computer and use it in GitHub Desktop.
// You'll need this https://gist.github.com/jorgenisaksson/76a8dae54fd3dc4e31c2
extension NSBezierPath {
func toCGPath () -> CGPath? {
if self.elementCount == 0 {
return nil
}
let path = CGPathCreateMutable()
var didClosePath = false
for i in 0...self.elementCount-1 {
var points = [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 CGPathCreateCopy(path)
}
}
var isFirst = false {
didSet {
if isFirst {
let mask = NSBezierPath(roundedRect: NSRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height + 10), xRadius: 10.0, yRadius: 10.0)
mask.fill()
let maskShape = CAShapeLayer()
maskShape.path = mask.toCGPath()
self.layer?.mask = maskShape
} else {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment