Question

I'm having a bit of a problem with a UITapGestureRecognizer. I create it this way:

self.userInteractionEnabled = YES;

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tapRecognizer.cancelsTouchesInView = NO;
tapRecognizer.delaysTouchesBegan = YES;
tapRecognizer.delegate = self;
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[self addGestureRecognizer:tapRecognizer];

In the header file I also include and implement the shouldReceiveTouch: method like so:

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

I've looked through countless threads of people with similar problems and can't seem to find a solution. I've heard that if the view you're adding the gesture recognizer has subviews with userInteractionEnabled set to YES, that could possibly interfere with the tap recognition, so I also include this:

for(UIView *subview in self.subviews) {
    subview.userInteractionEnabled = NO;
}

Anyone know why the gesture recognizer doesn't work?


Edit:

Here are some details:

  • I'm adding the UITapGestureRecognizer to a UIView subclass.
  • I add the gesture recognizer in the subclass' initWithFrame: method.
  • I've verified that the gesture recognizer is being added by stepping through the portion of the code where it's actually added.
  • The view controller that contains this view does not have any gesture recognizers attached to it, but does implement touchesBegan, touchesMoved, and touchesEnded. However, according to this question, that the view controller implements those shouldn't affect the gesture recognition of the view.


Edit 2:

I've verified that there are no other views blocking the view with the gesture recognizer from receiving touches. I've also verified that the view is actually being added to the containing view controller's view. It seems like the problem's elsewhere.

Was it helpful?

Solution

Your set up should work, so I would guess that one of two things is happening. You may have forgotten to actually add this view to the ViewController's view, or there maybe another view placed on top of this view which is stealing the touches?

OTHER TIPS

Another possibility causing the problem is that the view on which the UITapGestureRecognizer is added is not big enough, so when you tap on the screen, the touch point is not located in the view's bounds.

Also make sure the UIView is not transparent. Set the background color to e.g. black. This solved the problem for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top