11
votes

I've got an app that displays a page of text with the ability to tap a button or swipe in a view to advance or retreat through various pages. The container view has two UISwipeGestureRecognizers attached, for swipe left and swipe right. No trouble with these gestures. But now I'm trying to add UITapGestureRecognizer to another view, providing an ability similar to iBooks or the Kindle app, tap the left to go back, the right to go forward. Nothing I do can get the gesture to fire. No hint that it is ever being triggered, even if I put the gesture on my topmost view and disable other gestures.

The view controller implements UIGestureRecognizerDelegate, though I've not needed to implement delegate methods. I did try implementing shouldReceiveTouch: without success.

Here's a snippet of code where I create and attach the gesture recognizer:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureTurnPage:)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];
3
Could you please show more code? - xissburg
Have you confirmed that tapGestureTurnPage: gets triggered? - PengOne
xissburg, I understand more code would be helpful, but not sure what I would show. This is a completed app, I'm just adding some tweaks now for an update. Not sure which code might help indicate where the problem lies. - Chris Roberts
PengOne, no, it never gets triggered. The tap gesture never fires. Right now the only code in tapGestureTurnPage is a call to NSLog and nothing ever shows up. If that method fired then I wouldn't be posting here. :) - Chris Roberts
Chris Roberts, show how your view tree is organized, and what kind of events each of these views respond for (gestures and other stuff). - xissburg

3 Answers

26
votes

Try this on the view you're adding the gesture recognizers:

view.userInteractionEnabled = YES;
7
votes

You mention that you've tried this, but just in case go through it again.

Try using the delegate with <UIGestureRecognizerDelegate> in the header and then setting the delegate:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureTurnPage:)];
[self.view addGestureRecognizer:recognizer];
recognizer.delegate = self;
[recognizer release];

Then implement this method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
       shouldReceiveTouch:(UITouch *)touch {
    return YES;
}

Then use a debugger or toss an NSLog into the above method and also tapGestureTurnPage: to see if these methods are being called.

3
votes

Add this where you initialize the gesture:

recognizer.delegate = self;

and add this in the "self" class:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

This allows me to have gestures recognized on the UIWebView, and the UIWebView will still respond to the taps. (which i wanted - you may not)