1
votes

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.

3
Does one of this view's parent objects have a gesture recognizer attached to it?Brian Shamblen
@BrianShamblen - The view controller that the view is in doesn't have any gesture recognizers attached to it, but it does implement touchesBegan:, touchesMoved:, and touchesEnded:.pasawaya
Can you try to set the background color of the view to red? If you can see the red view fine, set the background color for all the views inside your hierarchy, then you can tell if the view is overlappedvodkhang

3 Answers

2
votes

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?

0
votes

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.

0
votes

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