diff --git a/touchbar_nyancat/NyanCatCanvas.swift b/touchbar_nyancat/NyanCatCanvas.swift index 785b615..e36852f 100644 --- a/touchbar_nyancat/NyanCatCanvas.swift +++ b/touchbar_nyancat/NyanCatCanvas.swift @@ -10,16 +10,39 @@ import Cocoa class NyanCatCanvas: NSImageView { + var xPosition: CGFloat = 0 { + didSet { + self.frame = CGRect(x: xPosition, y: 0, width: 685, height: 30) + } + } + override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. - self.frame = CGRect(x: 0, y: 0, width: 685, height: 30) - self.animates = true self.image = NSImage(named: "nyan_long@2x.gif")! self.canDrawSubviewsIntoLayer = true + self.frame = CGRect(x: xPosition, y: 0, width: 685, height: 30) + } + + override func touchesBegan(with event: NSEvent) { + // Calling super causes the cat to jump back to its original position 🤔 + //super.touchesBegan(with: event) + } + + override func touchesMoved(with event: NSEvent) { + if #available(OSX 10.12.1, *) { + let current = event.allTouches().first?.location(in: self).x ?? 0 + let previous = event.allTouches().first?.previousLocation(in: self).x ?? 0 + + let dX = (current - previous) + + xPosition += dX + } else { + // Fallback on earlier versions + } } }