如何将TouchUpInside事件传递给UIButton,而不将其传递给父视图



我有一个启用了分页的UIScrollview。此滚动视图中有3个视图(页面)。滚动视图的父视图上有一个点击手势,可以显示和隐藏顶部的导航栏。

问题:在其中一个页面中,我想添加按钮。但问题是,每当我点击这些按钮时,显示/隐藏导航栏方法也会被触发。只将触摸传递给这些按钮而不传递给滚动视图的父视图的最佳方式是什么?

NJones走在正确的轨道上,但我认为他的回答有一些问题。

我假设您想通过触摸滚动视图中的任何按钮。在手势识别器的代理中,实现gestureRecognizer:shouldReceiveTouch:,如下所示:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch {
    UIView *gestureView = recognizer.view;
    // gestureView is the view that the recognizer is attached to - should be the scroll view
    CGPoint point = [touch locationInView:gestureView];
    UIView *touchedView = [gestureView hitTest:point withEvent:nil];
    // touchedView is the deepest descendant of gestureView that contains point
    // Block the recognizer if touchedView is a UIButton, or a descendant of a UIButton
    while (touchedView && touchedView != gestureView) {
        if ([touchedView isKindOfClass:[UIButton class]])
            return NO;
        touchedView = touchedView.superview;
    }
    return YES;
}

相关内容

最新更新