如何在UIScrollView的superview中检测"Touch Down"?

wgp*_*ubs 7 objective-c uiscrollview uiview touch-event ios

我有一个UIView包含a UIScrollView,我希望能够UIView在用户点击的任何时候捕获"Touch Down"事件UIScrollView.

我已经尝试将所有touchesBegan/Ended/Cancelled处理程序包含在我的中,UIViewController但是当点击UIScrollViewmain中包含的内容时,它们都没有被触发UIView.

完成此任务的最佳方法是什么?

Ars*_*yev 2

在UIView中,实现touchesBegan:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // assign a UITouch object to the current touch
    UITouch *touch = [[event allTouches] anyObject];

    // if the view in which the touch is found is myScrollView
    // (assuming myScrollView is the UIScrollView and is a subview of the UIView)
    if ([touch view] == myScrollView) {
        // do stuff here
    }
}
Run Code Online (Sandbox Code Playgroud)

旁注:确保 UIView 中的 userInteractionEnabled 设置为 YES。

  • 这违背了 uiscrollview 的目的,因为我们必须在“do stuff here”下实现滚动操作。 (3认同)