How to recognize swipe in all 4 directions How to recognize swipe in all 4 directions ios ios

How to recognize swipe in all 4 directions


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)}


From the storyboard:

  1. Add four swipe gesture recognizers to your view.
  2. Set each one with the target direction from the attribute inspector. You canselect right, left, up or down
  3. One by one, select the swipe gesture recognizer, control + drag to your view controller. Insert the name (let us say leftGesture, rightGesture, upGesture anddownGesture), change the connection to: Action and type to:UISwipeGestureRecognizer

From your viewController:

@IBAction func rightGesture(sender: UISwipeGestureRecognizer) {    print("Right")}@IBAction func leftGesture(sender: UISwipeGestureRecognizer) {    print("Left")}@IBAction func upGesture(sender: UISwipeGestureRecognizer) {    print("Up")}@IBAction func downGesture(sender: UISwipeGestureRecognizer) {    print("Down")}