【问题标题】:Why tap gesture recognizer recognizes the taps from the view on its parent view?为什么点击手势识别器会从其父视图的视图中识别点击?
【发布时间】:2013-03-06 18:59:46
【问题描述】:

我有一个 UIView 和一个点击手势识别器:

UIImageView *tabView = [[UIImageView alloc] initWithFrame:CGRectMake(41, 145, 702, 100)];
tabView.image = [UIImage imageNamed:@"inactive_tab"];
tabView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
                                        action:@selector(handleSingleTap:)];
[tabView addGestureRecognizer:singleFingerTap];
[self.scrollView addSubview:tabView];

我在滚动视图上添加另一个视图:

[self.scrollView addSubview:self.activeTab];

activeTab 在 inactiveTap 之上。当我点击 activeTap 时,手势识别器会触发,我不想发生这种情况。我怎样才能避免这种情况?

【问题讨论】:

    标签: ios uigesturerecognizer


    【解决方案1】:

    使用UIGestureRecognizerDelegate 及其方法gestureRecognizer:shouldReceiveTouch:。 您可以检查触摸点是否在视图框架内,如果您不希望触摸发生在该视图上,则返回 NO。`

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        CGPoint touchLocation = [touch locationInView:self.view];
        return !CGRectContainsPoint(self.activeTab.frame, touchLocation);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多