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
  • Select an option

  • Save eonist/31dbc43ba8668726d2356dbc60b838cb to your computer and use it in GitHub Desktop.

Select an option

Save eonist/31dbc43ba8668726d2356dbc60b838cb to your computer and use it in GitHub Desktop.

Revisions

  1. eonist revised this gist Jan 15, 2017. No changes.
  2. eonist revised this gist Jan 15, 2017. No changes.
  3. eonist revised this gist Jan 15, 2017. 1 changed file with 14 additions and 15 deletions.
    29 changes: 14 additions & 15 deletions touch.swift
    Original file line number Diff line number Diff line change
    @@ -1,40 +1,39 @@
    import Cocoa
    import XCPlayground
    import PlaygroundSupport

    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)
    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(theEvent: NSEvent) {
    override func mouseDown(with event: NSEvent) {
    currentPath = NSBezierPath()
    currentPath.moveToPoint(theEvent.locationInWindow)
    currentPath.move(to: event.locationInWindow)
    }

    override func mouseDragged(theEvent: NSEvent) {
    currentPath.lineToPoint(theEvent.locationInWindow)
    override func mouseDragged(with event: NSEvent) {
    currentPath.line(to: event.locationInWindow)
    needsDisplay = true
    }

    override func mouseUp(theEvent: NSEvent) {
    path.appendBezierPath(currentPath)
    override func mouseUp(with event: NSEvent) {
    path.append(currentPath)
    currentPath = NSBezierPath()
    needsDisplay = true
    }
    }

    let touchView: TouchView = {
    $0.wantsLayer = true
    $0.layer?.backgroundColor = NSColor.whiteColor().CGColor
    $0.layer?.backgroundColor = NSColor.white.cgColor
    return $0
    }(TouchView(frame: NSRect(x: 0, y: 0, width: 300, height: 200)))

    XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
    XCPlaygroundPage.currentPage.liveView = touchView
    PlaygroundPage.current.needsIndefiniteExecution = true
    PlaygroundPage.current.liveView = touchView
  4. @erica erica created this gist Jan 26, 2016.
    40 changes: 40 additions & 0 deletions touch.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    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