Call another object's touches begin iOS

匿名 (未验证) 提交于 2019-12-03 08:56:10

问题:

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?

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!