Created
January 7, 2019 06:17
-
-
Save victorchee/dd5ae369a6e13d21dba61a66eacf5c74 to your computer and use it in GitHub Desktop.
Pan gesture recognizer with directions.
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
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