Skip to content

Instantly share code, notes, and snippets.

@eonist
Created December 10, 2015 16:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eonist/8eef7bef6492c2756028 to your computer and use it in GitHub Desktop.
Save eonist/8eef7bef6492c2756028 to your computer and use it in GitHub Desktop.
Quartz Outer shadow (playground)
import Cocoa
import XCPlayground
import XCTest
class FlippedView:NSView {//Organizes your view from top to bottom
override var flipped:Bool {
get {
return true
}
}
}
class Container:FlippedView{
var color:NSColor = NSColor.blueColor()
override var wantsDefaultClipping:Bool{return false}//avoids clipping the view
override var wantsUpdateLayer:Bool {return true}
init(_ width:Int = 400, _ height:Int = 400, _ color:NSColor = NSColor.blueColor() ) {
Swift.print("Container.init()")
self.color = color
let frame = NSRect(x: 0, y: 0, width: width, height: height)
super.init(frame: frame)
self.wantsLayer = false
}
override func drawRect(dirtyRect: NSRect) {
//Swift.print("drawRect()")
let nsctx:NSGraphicsContext/**/ = NSGraphicsContext.currentContext()!
let context/**/ = nsctx.CGContext//was graphicsPort
var path:CGMutablePathRef = CGPathCreateMutable();
let rectangle:CGRect = CGRectMake(20.0, 20.0,200.0, 200.0);
path = CGPathCreateWithRoundedRect(rectangle, 20, 20, nil) as! CGMutablePathRef
/* Add the rectangle to the path */
//CGPathAddRect(path,nil, rectangle);
CGContextAddPath(context,path)
CGContextSaveGState(context)
/*offset,blur and color*/
CGContextSetShadowWithColor(context, CGSizeMake(14, -14), 17.0, NSColor.grayColor().CGColor);//offset,bl
CGContextSetFillColorWithColor(context,NSColor.greenColor().CGColor)
CGContextDrawPath(context, CGPathDrawingMode.Fill)//FillStroke
CGContextRestoreGState(context);
/*add the stroke ontop*/
CGContextAddPath(context,path)
CGContextSetStrokeColorWithColor(context, NSColor.redColor().CGColor)
CGContextSetLineWidth(context, 15.0)
CGContextDrawPath(context, CGPathDrawingMode.Stroke)//FillStroke
}
/**
* Required by super class
*/
required init?(coder: NSCoder) {fatalError("init(coder:) has not been implemented")}
}
let container = Container()
XCPlaygroundPage.currentPage.liveView = container
@eonist
Copy link
Author

eonist commented Jan 29, 2016

Example:

screen shot 2015-12-08 at 05 35 28

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