Scrungepipes
Scrungepipes

Reputation: 37581

Swipe gesture recognizers not triggering method call

I'm adding a couple of swipe gesture recognizers in viewDidLoad however the specified selectors are not being called:

UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)];
swipeGestureRight.numberOfTouchesRequired = 1;
swipeGestureRight.direction = (UISwipeGestureRecognizerDirectionRight);
[self.scrollView addGestureRecognizer:swipeGestureRight];

UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
swipeGestureLeft.numberOfTouchesRequired = 1;
swipeGestureLeft.direction = (UISwipeGestureRecognizerDirectionLeft);
[self.scrollView addGestureRecognizer:swipeGestureLeft];

If I change the selectors to @selector(crashAtRunTimeWithUndefinedMethod:) then nothing happens. The view controller is inheriting from UIGestureRecognizerDelegate. Setting the delegate of the gesture recognizers is not done in the above code but added it and setting it to self doesn't make any difference.

The scrollView is child view of the controller's main view, but I tried changing the target to the main view and that makes no difference either.

Upvotes: 0

Views: 692

Answers (1)

bilobatum
bilobatum

Reputation: 8918

A UIScrollView already has pre-configured gesture recognizers that detect swipes (and other gestures, like panning). Log the array that holds the gestures recognizes of a UIScrollView and you will see them.

It's not that you can't add additional gesture recognizers to a UIScrollView, but you have to be careful. In my own app, I added a tap gesture recognizer to a UIScrollView without issue because it doesn't compete with any of the pre-configured gesture recognizers.

Upvotes: 1

Related Questions