can drag the cat

This commit is contained in:
Pawin Thepbanchaporn 2016-11-26 20:08:41 +07:00
parent f82560332e
commit 1c98a4b4d9

View file

@ -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
}
}
}