0

On parentClass(inheriting from UIView) i have:

[self addGestureRecognizer:_tapGesture]; // _tapGesture is UITapGestureRecognizer, with delegate on parentClass

On someClass:

[_myImageView addGestureRecognizer:_imageViewGestureRecognizer]; // _imageViewGestureRecognizer is UITapGestureRecognizer, with delegate on someClass

The problem is that when i tap on myImageView both gesture recognizers are firing. I want only _imageViewGestureRecognizer to work.

I've tried:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch {
   UIView *gestureView = recognizer.view;
   CGPoint point = [touch locationInView:gestureView];
   UIView *touchedView = [gestureView hitTest:point withEvent:nil];
   if ([touchedView isEqual:_imageViewGestureRecognizer]) {
     return NO;
   }

   return YES;
}

But it ofc doesn't take upon consideration gesture recognizer from super class.

Nat
  • 12,032
  • 9
  • 56
  • 103

1 Answers1

1

I did this little test and it worked perfectly...

@implementation View

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    self.backgroundColor = [UIColor whiteColor];

    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped1)];
    [self addGestureRecognizer:tap];

    UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"]];
    img.userInteractionEnabled = YES;
    img.frame = CGRectMake(0, 0, 100, 100);
    [self addSubview:img];

    UITapGestureRecognizer* tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped2)];
    [img addGestureRecognizer:tap2];

    return self;
}

-(void)tapped1 {
    NSLog(@"Tapped 1");
}

-(void)tapped2 {
    NSLog(@"Tapped 2");
}

@end

What you wanted was iOS's default behaviour. Once a subview has taken care of a touch, its superview won't receive the touch anymore. Did you set userInteractionEnabled on the imageView?

Khanh Nguyen
  • 11,112
  • 10
  • 52
  • 65
  • Hmm.. thanks for testing. I've got it set as userInteractionEnabled = YES, but in this case i have to look for problem elsewhere in code. Thanks once again, i'm marking this as answer ;) Now i'm wondering what can cause such a problem xD. – Nat Jun 24 '13 at 10:42
  • 1
    I've found solution, problem was from framework I've worked with. In case anyone occurs it: https://github.com/gmoledina/GMGridView/issues/68 – Nat Jun 25 '13 at 11:54