Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recognize swipe in all 4 directions

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..

like image 849
user3739367 Avatar asked Jun 13 '14 23:06

user3739367


People also ask

How to detect swipe gesture in Swift?

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(_:)

What is swipe gesture?

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.

What is swipe gesture on iPhone?

Swipe right or left along the bottom edge of the screen to quickly switch between open apps. See Switch between open apps on iPhone.


2 Answers

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         }     } } 
like image 127
Nate Cook Avatar answered Sep 23 '22 21:09

Nate Cook


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) } 
like image 43
Alexandre Cassagne Avatar answered Sep 22 '22 21:09

Alexandre Cassagne