0

在 parentClass(从 UIView 继承)我有:

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

在某些类上:

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

问题是当我点击 myImageView 时,两个手势识别器都在触发。我只想 _imageViewGestureRecognizer 工作。

我试过了:

- (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;
}

但是它没有考虑超类的手势识别器。

4

1 回答 1

1

我做了这个小测试,它工作得很好......

@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

你想要的是 iOS 的默认行为。一旦子视图处理了触摸,它的父视图将不再接收触摸。你userInteractionEnabled在imageView上设置了吗?

于 2013-06-24T10:36:26.753 回答