Created
January 26, 2016 18:41
-
-
Save erica/53c77b931a6ed560da5a to your computer and use it in GitHub Desktop.
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
import Cocoa | |
import XCPlayground | |
class TouchView: NSView { | |
var (path, currentPath) = (NSBezierPath(), NSBezierPath()) | |
override func drawRect(dirtyRect: NSRect) { | |
guard let contextPtr = NSGraphicsContext.currentContext()?.graphicsPort else {return} | |
let context = unsafeBitCast(contextPtr, CGContext.self) | |
CGContextClearRect(context, dirtyRect) | |
path.stroke() | |
currentPath.lineWidth = 2.0 | |
currentPath.stroke() | |
} | |
override func mouseDown(theEvent: NSEvent) { | |
currentPath = NSBezierPath() | |
currentPath.moveToPoint(theEvent.locationInWindow) | |
} | |
override func mouseDragged(theEvent: NSEvent) { | |
currentPath.lineToPoint(theEvent.locationInWindow) | |
needsDisplay = true | |
} | |
override func mouseUp(theEvent: NSEvent) { | |
path.appendBezierPath(currentPath) | |
currentPath = NSBezierPath() | |
needsDisplay = true | |
} | |
} | |
let touchView: TouchView = { | |
$0.wantsLayer = true | |
$0.layer?.backgroundColor = NSColor.whiteColor().CGColor | |
return $0 | |
}(TouchView(frame: NSRect(x: 0, y: 0, width: 300, height: 200))) | |
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true | |
XCPlaygroundPage.currentPage.liveView = touchView |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dm-katsubo:
Make sure you select „Platform: OS X“ when creating a new playground.