添加子视图后未收到touchesEnded/Moved/Cancelled
标题或多或少说明了一切。为了响应 touchesBegan
事件,我的 UIViewController
重新着色并添加一些子视图。
它永远不会收到 touchesEnded
。我猜是因为添加的子视图以某种方式拦截了该事件。我尝试在子视图上调用 resignFirstResponder
但无济于事。
当我不添加子视图并且触摸事件正常调用时,代码工作正常。
有什么想法吗?
谢谢
编辑:一些细节以及我如何修复它。
基本上我有一个带有一些子视图的主视图,当我触摸子视图时,事件将传递到主视图,但是,在这个事件中,我删除了子视图并在其位置添加了新的子视图。触摸源自不再存在的子视图这一事实意味着触摸的其余部分丢失了。
我通过在主视图中覆盖 hitTest:withEvent 来修复此问题,以阻止对子视图进行触摸测试
Title more or less says it all. In response to a touchesBegan
event, my UIViewController
recolours itself and adds some subviews.
It never receives the touchesEnded
. I guess because the added subviews are somehow intercepting the event. I tried calling resignFirstResponder
on the subviews to no avail.
The code works fine when I don't add the child views and the touch events are called as normal.
Any ideas?
Thanks
EDIT: Bit of detail and how I fixed it.
Basically I had a master view with some subviews, when I touched the subview, the event would be passed through to the master view, however, on this event I was removing the subviews and adding new ones in their place. The fact that the touch originated on a subview which no longer existed meant that the rest of the touch was lost.
I fixed this by overriding hitTest:withEvent in my master view, to stop touches ever getting tested against the subviews
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在将子视图添加为子视图之前,您是否尝试将子视图的
userInteractionEnabled
属性设置为 NO ?Did you try to set the
userInteractionEnabled
property to NO for the subview before adding it as a subview ?您需要使用如下内容将触摸从子视图传递到超级视图:
You're going to need pass the touch from the subview onto the superview using something like this: