Skip to content

Instantly share code, notes, and snippets.

@mukyasa
Last active November 7, 2017 11:46
Show Gist options
  • Select an option

  • Save mukyasa/0201431f735c51eec190d1f9f35dc9d7 to your computer and use it in GitHub Desktop.

Select an option

Save mukyasa/0201431f735c51eec190d1f9f35dc9d7 to your computer and use it in GitHub Desktop.
//Pan Gesture
public enum PanDirection: Int {
case up, down, left, right
public var isVertical: Bool { return [.up, .down].contains(self) }
public var isHorizontal: Bool { return !isVertical }
}
public extension UIPanGestureRecognizer {
public var direction: PanDirection? {
let velocity = self.velocity(in: view)
let isVertical = fabs(velocity.y) > fabs(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