0

Consider the following code:

@interface TouchDownGestureRecognizer : UIGestureRecognizer
@end

@implementation TouchDownGestureRecognizer
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesBegan");
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesMoved");
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesEnded");
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesCancelled");
}
@end

In the constructor for a class that derives from UIView

- (id)initWithFrame:(CGRect)frame
{
    <snip>

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [tapRecognizer setDelegate:self];
    [self addGestureRecognizer:tapRecognizer];

    TouchDownGestureRecognizer *touchDownRecognizer = [[TouchDownGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchDown:)];
    [self addGestureRecognizer:touchDownRecognizer];

    <snip>
}

An object of this class is added to a parent view, and for the most part, tapping the subview causes touchesBegan, touchesEnded, and handleTap to get called. Under some circumstance (which I have not been able to pinpoint), handleTap stops getting called for the subview, (and the parent's handleTap gets called instead). However, even though handleTap stops getting called, touchesBegan and touchedEnded continue to get called for the subview. I have ensured that the UITapGestureRecognizer is still in the subview's gestureRecognizers array. I have also ensured that the userInteractionEnabled property for the subview is YES. Is there some known condition or state of the UIView where we would expect this behavior?

Carl
  • 301
  • 4
  • 13
  • Appears to be the same issue as in http://stackoverflow.com/questions/19095165/should-superviews-gesture-cancel-subviews-gesture-in-ios-7. – Carl Jun 11 '14 at 15:16

1 Answers1

0

So if I understand correctly, at some point the subview's parent's UITapGestureRecognizer is calling it's "handleTap" selector. This is probably occurring when you tap outside of the subview's bounds. A UIView's bounds are different from it's frame in that it's bounds determine where the UIView recieves touch events while the frame determines where the content is drawn. A UIView's bounds are independent of its frame so it's possible you're touching the parent even if you're within the frame of the subview.

Problem is, I don't think the subview's touchesBegan and touchesEnded would be called if this was the case but it's a place to start if you haven't checked that already. Maybe the GestureRecognizer still receives the event because it's in the view's hierarchy (think of it bubbling up) but since it isn't responsible for that event, it doesn't call handleTap... but that's outside my scope and understanding.

Literphor
  • 498
  • 4
  • 16