Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Jan 29, 2016

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

@dirk68-fu

This comment has been minimized.

Copy link

commented Feb 16, 2016

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

@eonist

This comment has been minimized.

Copy link

commented Jan 15, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.