Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save longpham2310/850ba96619dd151ed89dcbda10524790 to your computer and use it in GitHub Desktop.
Save longpham2310/850ba96619dd151ed89dcbda10524790 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