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

时间:2012-01-22 00:07:31

标签: objective-c uiscrollview gesture-recognition

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

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

1 个答案:

答案 0 :(得分:1)

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