Skip to content

Instantly share code, notes, and snippets.

@ppth0608
Created July 28, 2019 07:08
Show Gist options
  • Save ppth0608/833ace61a3a68c790c03ebbfc9be0efb to your computer and use it in GitHub Desktop.
Save ppth0608/833ace61a3a68c790c03ebbfc9be0efb to your computer and use it in GitHub Desktop.
How to make `direction` of pan gesture
import UIKit
extension UIPanGestureRecognizer {
enum Direction: String {
case up
case down
case left
case right
var isVertical: Bool {
switch self {
case .up, .down: return true
case .left, .right: return false
}
}
var isHorizontal: Bool {
switch self {
case .up, .down: return false
case .left, .right: return true
}
}
}
var direction: Direction? {
let velocity = self.velocity(in: view)
let isVertical = abs(velocity.y) > abs(velocity.x)
switch (isVertical, velocity.x, velocity.y) {
case (true, _, let y) where y < 0: return .up
case (true, _, let y) where y > 0: return .down
case (false, let x, _) where x > 0: return .right
case (false, let x, _) where x < 0: return .left
default: return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment