How to detect Swipe Gesture in iOS?

Parth Bhatt picture Parth Bhatt · Nov 25, 2010 · Viewed 43.2k times · Source

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?

Answer

Guntis Treulands picture Guntis Treulands · Oct 1, 2012

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>