Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swipe gesture in Swift 3

Tags:

swift

Im trying to get a UISwipeGestureRecognizer to work in Swift 3, the default swipe right is working correctly though not up down or left.

I have tried it by control dragging an Action

@IBAction func hole(_ recognizer: UISwipeGestureRecognizer) {     if (recognizer.direction == UISwipeGestureRecognizerDirection.left)     {         print("left")     }else if recognizer.direction == .right {         print("right")     }else {         print("other")     } } 

And, in ViewDidLoad

//gesture recognisers let swipeRight = UISwipeGestureRecognizer(target: self, action: "holeSwiped:") swipeRight.direction = UISwipeGestureRecognizerDirection.right self.view.addGestureRecognizer(swipeRight)  let swipeLeft = UISwipeGestureRecognizer(target: self, action: "holeSwiped:") swipeLeft.direction = UISwipeGestureRecognizerDirection.left self.view.addGestureRecognizer(swipeLeft) 

to my method

func holeSwiped(gesture: UISwipeGestureRecognizer) {     if let swipeGesture = gesture as? UISwipeGestureRecognizer{         switch swipeGesture.direction {         case UISwipeGestureRecognizerDirection.right:             print("right swipe")         case UISwipeGestureRecognizerDirection.left:             print("left swipe")         default:             print("other swipe")         }     } } 

Now, none of the swipes are working except the default right. Any ideas?

like image 685
user1908962 Avatar asked Sep 29 '16 07:09

user1908962


People also ask

How do I add swipe gestures in Swift?

Fire up Xcode and create a blank project by choosing the App template from the iOS > Application section. Name the project Swipes, set Interface to Storyboard, and Language to Swift. Open ViewController. swift and declare a private, constant property with name swipeableView of type UIView .

What is the swipe gesture?

This gesture is also called tap and hold and can be used to activate special menus. A swipe is is when you touch and slide your finger across the screen. You can swipe quickly or slowly, depending on what you're doing on your phone or tablet.

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.


1 Answers

Step 1: Add swipe Gesture(s) in viewDidLoad() method.

override func viewDidLoad() {     super.viewDidLoad()              let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))     swipeLeft.direction = .left     self.view.addGestureRecognizer(swipeLeft)              let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))     swipeRight.direction = .right     self.view.addGestureRecognizer(swipeRight)              let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))     swipeUp.direction = .up     self.view.addGestureRecognizer(swipeUp)              let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))     swipeDown.direction = .down     self.view.addGestureRecognizer(swipeDown) } 

Step 2: Check the gesture detection in handleGesture() method

func handleGesture(gesture: UISwipeGestureRecognizer) {    if gesture.direction == .right {         print("Swipe Right")    }    else if gesture.direction == .left {         print("Swipe Left")    }    else if gesture.direction == .up {         print("Swipe Up")    }    else if gesture.direction == .down {         print("Swipe Down")    } } 

I hope this will help someone.

UPDATE:

You need to use @objc to call your 'Selector' method for latest swift versions. ie,

@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {   .   .   . } 
like image 196
Ram Madhavan Avatar answered Sep 27 '22 20:09

Ram Madhavan