Merge pull request #9 from pawin/master

can drag the cat
This commit is contained in:
Vatsaev Aslan 2016-11-26 16:28:00 +01:00 committed by GitHub
commit 8b302b1e44

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