调用另一个对象的触摸开始 iOS [英] Call another object's touches begin iOS

查看:20
本文介绍了调用另一个对象的触摸开始 iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有拦截触摸事件的 view1 和拦截触摸事件的 view2 .

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

view1 能否将这些事件传递给 view2 并调用 [view2 touchesBegin] [view2 touchesMoved]...等?

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

推荐答案

是的,有时,也许.您所询问的技术称为事件转发.我会向您介绍 转发触摸事件 部分 iOS 事件处理指南,内容如下:

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:

UIKit 框架的类不是为了接收触摸而设计的不受他们约束的;在程序方面,这意味着UITouch 对象的 view 属性必须持有对框架对象,以便处理触摸.如果你想有条件地将触摸转发给应用程序中的其他响应者,所有这些响应者都应该是你自己的子类的实例界面视图.

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.

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

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:

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

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.

这篇关于调用另一个对象的触摸开始 iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆