【问题标题】:How to receive all touch events that occur in a view hierarchy?如何接收视图层次结构中发生的所有触摸事件?
【发布时间】:2025-04-06 07:00:01
【问题描述】:

我有一个由 UIViewController 控制的复杂视图层次结构。我正在寻找一种简单的方法来通知控制器层次结构内发生的所有触摸,即使是那些 由子视图处理的触摸。我不想拦截它们,我只想了解它们。

并且子类化层次结构中的所有视图并不是一个真正的选择。

谢谢!

【问题讨论】:

    标签: iphone cocoa-touch uiview uiviewcontroller touch


    【解决方案1】:

    在控制器中子类化根视图并在其中实现hitTest:withEvent: 方法:

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
        UIView *result = [super hitTest:point withEvent:event];
        // Your custom code
        return result;
    }
    

    【讨论】: