Proper Swipe gesture recognizer iOS

iamruskie picture iamruskie · Sep 14, 2012 · Viewed 20.5k times · Source

I haven't been able to find a tutorial on how to properly setup a gesture recognizer for iOS. I need to detect swipe up & down, and the callbacks for them.

Any help, appreciated. Thanks.

Answer

sergio picture sergio · Sep 14, 2012

You need two recognizers, one for swiping up, and the other for swiping down:

UISwipeGestureRecognizer* swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUpFrom:)];
swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;

and for the handler:

- (void)handleSwipeUpFrom:(UIGestureRecognizer*)recognizer {

}

Finally, you add it to your view:

[view addGestureRecognizer:swipeUpGestureRecognizer];

The same for the other direction (just change all the Ups to Downs).