I need to use swipe to recognize swipe gesture down and then right. But on swift UISwipeGestureRecognizer has predeterminate Right direction.. And I don't know how make this for use other directions..
A swipe gesture recognizer detects swipes in one of four directions, up , down , left , and right . We set the direction property of the swipe gesture recognizer to down . If the user swipes from the top of the blue view to the bottom of the blue view, the swipe gesture recognizer invokes the didSwipe(_:)
One of the most common gestures you will use on your smartphone or tablet is a swipe. That's where you place your finger on the screen and slide it across the surface. In most instances, the item under your finger will move. How fast the item moves depends on how fast you swipe.
Swipe right or left along the bottom edge of the screen to quickly switch between open apps. See Switch between open apps on iPhone.
You need to have one UISwipeGestureRecognizer
for each direction. It's a little weird because the UISwipeGestureRecognizer.direction
property is an options-style bit mask, but each recognizer can only handle one direction. You can send them all to the same handler if you want, and sort it out there, or send them to different handlers. Here's one implementation:
override func viewDidLoad() { super.viewDidLoad() let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture)) swipeRight.direction = .right self.view.addGestureRecognizer(swipeRight) let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture)) swipeDown.direction = .down self.view.addGestureRecognizer(swipeDown) } @objc func respondToSwipeGesture(gesture: UIGestureRecognizer) { if let swipeGesture = gesture as? UISwipeGestureRecognizer { switch swipeGesture.direction { case .right: print("Swiped right") case .down: print("Swiped down") case .left: print("Swiped left") case .up: print("Swiped up") default: break } } }
Swift 3:
override func viewDidLoad() { super.viewDidLoad() let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture)) swipeRight.direction = UISwipeGestureRecognizerDirection.right self.view.addGestureRecognizer(swipeRight) let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture)) swipeDown.direction = UISwipeGestureRecognizerDirection.down self.view.addGestureRecognizer(swipeDown) } func respondToSwipeGesture(gesture: UIGestureRecognizer) { if let swipeGesture = gesture as? UISwipeGestureRecognizer { switch swipeGesture.direction { case UISwipeGestureRecognizerDirection.right: print("Swiped right") case UISwipeGestureRecognizerDirection.down: print("Swiped down") case UISwipeGestureRecognizerDirection.left: print("Swiped left") case UISwipeGestureRecognizerDirection.up: print("Swiped up") default: break } } }
I just felt like contributing this, looks more elegant in the end:
func addSwipe() { let directions: [UISwipeGestureRecognizerDirection] = [.Right, .Left, .Up, .Down] for direction in directions { let gesture = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipe:")) gesture.direction = direction self.addGestureRecognizer(gesture) } } func handleSwipe(sender: UISwipeGestureRecognizer) { print(sender.direction) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With