Created
July 28, 2019 07:08
-
-
Save ppth0608/833ace61a3a68c790c03ebbfc9be0efb to your computer and use it in GitHub Desktop.
How to make `direction` of pan gesture
This file contains 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 | |
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