调用另一个对象的触摸开始 iOS

发布于 2024-12-19 04:51:03 字数 121 浏览 2 评论 0原文

假设我有 view1 正在拦截触摸事件,而 view2 则没有。

view1 可以将这些事件传递给 view2 并调用 [view2 TouchesBegin] [view2 TouchMoved]...等吗?

Lets say I have view1 which is intercepting touch events and view2 which is not.

Can view1 pass those events to view2 and call [view2 touchesBegin] [view2 touchesMoved]...etc?

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

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

发布评论

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

评论(1

飘落散花 2024-12-26 04:51:03

是的,有时,也许。您所询问的技术称为事件转发。我将向您推荐转发触摸事件 iOS 事件处理指南,内容如下:

UIKit 框架的类不是为接收触摸而设计的
不受他们约束的;从程序化的角度来看,这意味着
UITouch 对象的 view 属性必须保存对
框架对象以便处理触摸。如果你想
有条件地将触摸转发给应用程序中的其他响应者,
所有这些响应者都应该是您自己的子类的实例
UIView。

因此,如果您希望将事件从一个视图转发到另一个视图,并且两个视图都是您自己的 UIView 子类的实例,则事件转发可能适合您。如果您的 view2 是 UIKit 类的实例(例如 UIScrollView 或 UITextView),那么您遇到问题就不会感到惊讶。即使它现在有效,将来也很容易坏掉。在正文中,该部分更简单地说明了这一点:

不要将事件转发到 UIKit 框架对象。

我强烈建议您阅读整个部分——如果您决定使用事件转发,其中有一些好的建议和示例可能会有所帮助。

Yes, sometimes, maybe. The technique you're asking about is known as event forwarding. I'll refer you to the Forwarding Touch Events section of the Event Handling Guide for iOS, which says the following:

The classes of the UIKit framework are not designed to receive touches
that are not bound to them; in programmatic terms, this means that the
view property of the UITouch object must hold a reference to the
framework object in order for the touch to be handled. If you want to
conditionally forward touches to other responders in your application,
all of these responders should be instances of your own subclasses of
UIView.

So, if you're looking to forward events from one view to another and both views are instances of your own UIView subclass, event forwarding may work for you. If your view2 is an instance of a UIKit class, though -- say, UIScrollView or UITextView, you shouldn't be surprised to encounter problems. And even if it works now, it could easily break in the future. A little further on in the text, that section states this more simply:

Do not forward events to UIKit framework objects.

I'd urge you to read the entire section -- there's some good advice and an example that may help if you do decide to use event forwarding.

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