Skip to content

Instantly share code, notes, and snippets.

@eonist
Forked from erica/touch.swift
Last active September 5, 2017 22:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eonist/31dbc43ba8668726d2356dbc60b838cb to your computer and use it in GitHub Desktop.
Save eonist/31dbc43ba8668726d2356dbc60b838cb to your computer and use it in GitHub Desktop.
Swift 3 update (playground oriented programming)
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
@eonist
Copy link
Author

eonist commented Jan 15, 2017

playground oriented programming is definitely the future.

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