Search This Blog

Monday, October 30, 2017

iOS Swift. Swipe gesture recogniser in multiple directions

For swiping in each direction we need to create Swipe gesture recognizer and specify direction that we need for this recognizer. For each direction we need create separate recognizer.
override func viewDidLoad() {
    super.viewDidLoad()
        
    // Swipe Left
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swipingLeft))
    swipeLeft.direction = .left
    self.view.addGestureRecognizer(swipeLeft)
        
    // Swipe Right
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swipingRight))
    swipeRight.direction = .right
    self.view.addGestureRecognizer(swipeRight)
}
    
@objc func swipingLeft(sender: UISwipeGestureRecognizer) {
    print("Swipe Left")
}
    
@objc func swipingRight(sender: UISwipeGestureRecognizer) {
    print("Swipe Right")
}

We can make code more universal. When we swiping in different directions we can handle all this swipes in one method and switch between directions.
override func viewDidLoad() {
    super.viewDidLoad()
        
    let swipeRight = UISwipeGestureRecognizer(target: self,
                                              action: #selector(self.handleSwipe))
    swipeRight.direction = UISwipeGestureRecognizerDirection.right
    self.view.addGestureRecognizer(swipeRight)
        
    let swipeDown = UISwipeGestureRecognizer(target: self,
                                             action: #selector(self.handleSwipe))
    swipeDown.direction = UISwipeGestureRecognizerDirection.down
    self.view.addGestureRecognizer(swipeDown)
}
    
@objc func handleSwipe(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
        }
    }
}

4 comments:

  1. Really amazing information you have shared i was looking for this kind of unique information please do share that kind of unique information more as you can.
    -Web Development Services

    ReplyDelete
  2. This is an excellent article! Thank you a lot. I'm in desperate need of these suggestions. The in-depth explanation is quite beneficial. It was incredibly encouraging to see how you write. Best Custom Websites

    ReplyDelete
  3. Your article is unbelievable with precise knowledge. I can see a lot of research behind that and that is beyond my expectations.
    Create Your Own Website \

    ReplyDelete
  4. This is a great article! Thank you very much. I really need these suggestions. An in-depth explanation is of great benefit. Incredibly inspiring to see how you write. Custom Website

    ReplyDelete