Created
December 10, 2015 16:53
-
-
Save eonist/8eef7bef6492c2756028 to your computer and use it in GitHub Desktop.
Quartz Outer shadow (playground)
This file contains hidden or 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
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: