Skip to content

Instantly share code, notes, and snippets.

@erica
Created January 26, 2016 18:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save erica/53c77b931a6ed560da5a to your computer and use it in GitHub Desktop.
Save erica/53c77b931a6ed560da5a to your computer and use it in GitHub Desktop.
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
@dm-katsubo
Copy link

Error -> error: no such module 'Cocoa' (

@dirk68-fu
Copy link

@dm-katsubo:
Make sure you select „Platform: OS X“ when creating a new playground.

@eonist
Copy link

eonist commented Jan 15, 2017

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