如何检测“着陆”在 UIScrollView 的超级视图中?

发布于 2024-08-28 05:32:38 字数 334 浏览 3 评论 0原文

我有一个包含 UIScrollViewUIView,并且我希望能够在用户任何时候捕获 UIView 中的“Touch Down”事件点击 UIScrollView

我尝试在 UIViewController 中包含所有 TouchBegan/Ended/Cancelled 处理程序,但是当点击主 UIView< 中包含的 UIScrollView 内部时,它们都不会被触发。 /代码>。

实现这一目标的最佳方法是什么?

I have a UIView that contains a UIScrollView and I want to be able to capture the "Touch Down" event in the UIView any time the user taps on the UIScrollView.

I've tried including all the touchesBegan/Ended/Cancelled handlers in my UIViewController but none of them get fired when tapping inside the UIScrollView contained in the main UIView.

What is the best way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

辞旧 2024-09-04 05:32:38

在 UIView 中,实现 TouchBegan:

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

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

In the UIView, implement 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
    }
}

A side note: make sure userInteractionEnabled is set to YES in the UIView.

鸠书 2024-09-04 05:32:38

您还可以在 UIView 子类中实现 hitTest:withEvent:。调用此方法以确定哪个子视图应接收触摸事件。因此,在这里您可以只跟踪通过您的视图的所有事件,也可以隐藏子视图中的某些事件。在这种情况下,您可能不需要禁用滚动视图的用户交互。

有关此方法的更多详细信息,请参阅 UIView 类参考。

You can also implement hitTest:withEvent: in your UIView subclass. This method gets called to determine which subview should receive touch event. So here you can either just track all events passing through your view not or hide some of the events from subviews. In this case you may not need to disable user interaction for your scrollview.

See more details on this method in UIView class reference.

萌逼全场 2024-09-04 05:32:38

您还可以将手势识别器添加到您的超级视图中。
例如,如果您需要激活/停用诸如覆盖滚动视图的按钮之类的内容,请使用点击手势:

self.tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)] autorelease];
tap.numberOfTapsRequired = 1;

["containerView" addGestureRecognizer:tap];

手势确实会保留滚动视图交互

You can also add a gesture recognizer to your superview.
For example a tap gesture if you need to activate/deactivate things like buttons overlaying the scroll view :

self.tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)] autorelease];
tap.numberOfTapsRequired = 1;

["containerView" addGestureRecognizer:tap];

Gestures do preserve the scroll view interaction

找个人就嫁了吧 2024-09-04 05:32:38

您需要禁用用户与滚动视图的交互...

scrollView.userInteractionEnabled = NO;

一旦禁用,UIScrollView 的超级视图就会获取 TouchBegan 事件。

You need to disable user interaction with the scroll view as such ...

scrollView.userInteractionEnabled = NO;

Once disabled, the UIScrollView's superview gets the touchesBegan event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文