-
-
Save eonist/31dbc43ba8668726d2356dbc60b838cb to your computer and use it in GitHub Desktop.
Swift 3 update (playground oriented programming)
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 PlaygroundSupport | |
class TouchView: NSView { | |
var (path, currentPath) = (NSBezierPath(), NSBezierPath()) | |
override func draw(_ dirtyRect: NSRect) { | |
guard let contextPtr = NSGraphicsContext.current()?.graphicsPort else {return} | |
let context = unsafeBitCast(contextPtr, to: CGContext.self) | |
context.clear(dirtyRect) | |
path.stroke() | |
currentPath.lineWidth = 2.0 | |
currentPath.stroke() | |
} | |
override func mouseDown(with event: NSEvent) { | |
currentPath = NSBezierPath() | |
currentPath.move(to: event.locationInWindow) | |
} | |
override func mouseDragged(with event: NSEvent) { | |
currentPath.line(to: event.locationInWindow) | |
needsDisplay = true | |
} | |
override func mouseUp(with event: NSEvent) { | |
path.append(currentPath) | |
currentPath = NSBezierPath() | |
needsDisplay = true | |
} | |
} | |
let touchView: TouchView = { | |
$0.wantsLayer = true | |
$0.layer?.backgroundColor = NSColor.white.cgColor | |
return $0 | |
}(TouchView(frame: NSRect(x: 0, y: 0, width: 300, height: 200))) | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
PlaygroundPage.current.liveView = touchView |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
playground oriented programming is definitely the future.