Created
December 3, 2018 09:30
-
-
Save ryanmcgrath/64e07a0b914b00350a1a641b11f02ef4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func touchesMoved(with event: NSEvent) { | |
if(!isTracking) { return } | |
let currentTouches = event.touches(matching: .touching, in: view) | |
if(currentTouches.count != 2) { return } | |
currentTouchOne = nil | |
currentTouchTwo = nil | |
currentTouches.forEach { (touch: NSTouch) in | |
if(touch.identity.isEqual(initialTouchOne?.identity)) { | |
currentTouchOne = touch | |
} else { | |
currentTouchTwo = touch | |
} | |
} | |
let initialXPoint = [ | |
initialTouchOne?.normalizedPosition.x ?? 0.0, | |
initialTouchTwo?.normalizedPosition.x ?? 0.0 | |
].min() ?? 0.0 | |
let currentXPoint = [ | |
currentTouchOne?.normalizedPosition.x ?? 0.0, | |
currentTouchTwo?.normalizedPosition.x ?? 0.0 | |
].min() ?? 0.0 | |
let deviceWidth = initialTouchOne?.deviceSize.width ?? 0.0 | |
let oldX = (initialXPoint * deviceWidth).rounded(.up) | |
let newX = (currentXPoint * deviceWidth).rounded(.up) | |
var delta: CGFloat = 0.0 | |
if(oldX > newX) { // Swiping left | |
delta = (oldX - newX) * -1.0 | |
} else if(newX > oldX) { // Swiping right | |
delta = newX - oldX | |
} | |
NSAnimationContext.runAnimationGroup { [weak self] (context: NSAnimationContext) in | |
context.timingFunction = CAMediaTimingFunction(name: .easeIn) | |
context.duration = 0.2 | |
context.allowsImplicitAnimation = true | |
self?.leftAnchor?.animator().constant = delta | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment