Skip to content

Instantly share code, notes, and snippets.

@victorchee
Created January 7, 2019 06:17
Show Gist options
  • Save victorchee/dd5ae369a6e13d21dba61a66eacf5c74 to your computer and use it in GitHub Desktop.
Save victorchee/dd5ae369a6e13d21dba61a66eacf5c74 to your computer and use it in GitHub Desktop.
Pan gesture recognizer with directions.
import UIKit
enum PanDirection {
case vertical
case horizontal
}
class PanDirectionGestureRecognizer: UIPanGestureRecognizer {
let direction: PanDirection
init(direction: PanDirection, target: Any?, action: Selector?) {
self.direction = direction
super.init(target: target, action: action)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesMoved(touches, with: event)
if state == .began {
let velo = velocity(in: view)
switch direction {
case .horizontal where abs(velo.y) > abs(velo.x):
state = .cancelled
case .vertical where abs(velo.x) > abs(velo.y):
state = .cancelled
default:
break
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment