添加子视图后未收到touchesEnded/Moved/Cancelled

发布于 2024-08-25 09:59:25 字数 470 浏览 12 评论 0原文

标题或多或少说明了一切。为了响应 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 技术交流群。

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

发布评论

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

评论(2

很酷不放纵 2024-09-01 09:59:25

在将子视图添加为子视图之前,您是否尝试将子视图的 userInteractionEnabled 属性设置为 NO ?

Did you try to set the userInteractionEnabled property to NO for the subview before adding it as a subview ?

网白 2024-09-01 09:59:25

您需要使用如下内容将触摸从子视图传递到超级视图:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [super touchesBegan:touches withEvent:event];
}

You're going to need pass the touch from the subview onto the superview using something like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

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