Swipe gesture for all direction iOS swift

Solution:

In iOS you can add a gesture to a particular view in all  4 directions left,right,top and bottom.

override func viewDidLoad() {
    super.viewDidLoad()
    var rightAction= UISwipeGestureRecognizer(target: self, action: "respondSwipe:")
    rightAction.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(rightAction)

    var downAction= UISwipeGestureRecognizer(target: self, action: "respondSwipe:")
   downAction.direction = UISwipeGestureRecognizerDirection.Down
    self.view.addGestureRecognizer(downAction)

    var leftAction= UISwipeGestureRecognizer(target: self, action: "respondSwipe:")
   leftAction.direction = UISwipeGestureRecognizerDirection.Left
    self.view.addGestureRecognizer(leftAction)


    var upAction= UISwipeGestureRecognizer(target: self, action: "respondSwipe:")
   upAction.direction = UISwipeGestureRecognizerDirection.Up
    self.view.addGestureRecognizer(upAction)

}

The below method was called when you swipe in all directions.You can handle the actions when the user swipe in the below method.

func respondSwipe(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.Right:
                print("right")
            case UISwipeGestureRecognizerDirection.Down:
                print("down")
            case UISwipeGestureRecognizerDirection.Left:
                print("left")
            case UISwipeGestureRecognizerDirection.Up:
                print("up")
            default:
                break
        }
    }
}




Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift