How to detect Swipe Gesture in iOS?
Asked Answered
E

3

33

In my iPhone app, I require to recognize the swipe gesture made by the user on the view.

I want the swipe gestures to be recognized and perform a function on swipe.

I need that the view should horizontally slide and show another view as a user makes a swipe gesture.

What needs to be done?

How do I recognize it?

Evenfall answered 25/11, 2010 at 17:47 Comment(0)
L
40

Use the UISwipeGestureRecognizer. Not much else to say really, gesture recognizers are easy. There are WWDC10 videos on the subject even. Sessions 120 and 121. :)

Leucippus answered 25/11, 2010 at 17:50 Comment(2)
thanks for your input. Can you please give some demo or tutorial for this? I am a newbie. ThanksEvenfall
I can, but I'd rather you watch the WWDC10 videos, the two sessions I described. They will teach you everything you need to know about gesture recognizers in 2 hours. Much more efficiently than I could do here. I've updated my answer to provide a link to the videos.Leucippus
K
44

If You know how it works, but still need a quick example, here it is! (it will become handy at least for me, when I will need copy-paste example, without trying remembering it)

UISwipeGestureRecognizer *mSwipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething)];

[mSwipeUpRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];

[[self view] addGestureRecognizer:mSwipeUpRecognizer];

and in .h file add:

<UIGestureRecognizerDelegate>
Kleptomania answered 1/10, 2012 at 19:46 Comment(4)
[mSwipeUpRecognizer release]; is not necessary since the ARC's release, you should edit your answer. Regardless +1 for your answer.Adiell
self.swipeGestureRecognizer.direction = (UISwipeGestureRecognizerDirection.Left | UISwipeGestureRecognizerDirection.Right) // For your information: it does not work in Swift.Adiell
and in .h file add (not mandatory in Swift): <UIGestureRecognizerDelegate>Adiell
The above answer does not work in Swift, so please see my answer below (solution to the question in Swift and Objective-C).Adiell
L
40

Use the UISwipeGestureRecognizer. Not much else to say really, gesture recognizers are easy. There are WWDC10 videos on the subject even. Sessions 120 and 121. :)

Leucippus answered 25/11, 2010 at 17:50 Comment(2)
thanks for your input. Can you please give some demo or tutorial for this? I am a newbie. ThanksEvenfall
I can, but I'd rather you watch the WWDC10 videos, the two sessions I described. They will teach you everything you need to know about gesture recognizers in 2 hours. Much more efficiently than I could do here. I've updated my answer to provide a link to the videos.Leucippus
A
1

The following link below redirects you to a video tutorial which explains you how to detect swipes on the iPhone in Objective-C:

UISwipeGestureRecognizer Tutorial (Detecting swipes on the iPhone)

Code sample below, to achieve that in Swift:

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()
    
    var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)
    
    var swipeDown = UISwipeGestureRecognizer(target: self, action: "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:
            println("Swiped right")
        case UISwipeGestureRecognizerDirection.Down:
            println("Swiped down")
        default:
            break
        }
    }
}

Swift 5 version:

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGestureRight))
swipeRight.direction = UISwipeGestureRecognizer.Direction.right
self.view.addGestureRecognizer(swipeRight)
Adiell answered 8/5, 2015 at 16:42 Comment(1)
Why down voting when it works perfectly and could help people all around the world?Adiell

© 2022 - 2024 — McMap. All rights reserved.