Skip to content

Instantly share code, notes, and snippets.

@ryanmcgrath
Created December 3, 2018 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanmcgrath/64e07a0b914b00350a1a641b11f02ef4 to your computer and use it in GitHub Desktop.
Save ryanmcgrath/64e07a0b914b00350a1a641b11f02ef4 to your computer and use it in GitHub Desktop.
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