简体   繁体   English

如何获取当前活动的UIEvent?

[英]How to get currently active UIEvent?

Background 背景

I have a custom UIWindow implementation that posts a notification in the sendEvent method. 我有一个自定义的UIWindow实现,该实现在sendEvent方法中发布通知。 Then I have a custom view that, once added to the window hierarchy, removes itself from superview as soon as the mentioned notification is posted (ie tapping anywhere, in this view or not, removes the view). 然后,我有一个自定义视图,该视图一旦添加到窗口层次结构中,便会在上述通知发布后即从超级视图中将其自身删除(即,在此视图中的任意位置(无论是否在此视图中单击)都将删除该视图)。 Finally, I have a button that causes this custom view to be added to the view hierarchy. 最后,我有一个按钮,该按钮导致将此自定义视图添加到视图层次结构中。 Now the problem is that when I tap this button, the view gets added to the view hierarchy, BUT, the event that was caused by this tap reaches my custom UIWindow sendEvent method AFTER the custom view is added, thus resulting in the custom view being removed immediately after it has been added. 现在的问题是,当我点击此按钮时,视图被添加到视图层次结构BUT中,因此,在添加了自定义视图之后,由此点击引起的事件将到达我的自定义UIWindow sendEvent方法。添加后立即删除。

Question

I want to somehow access whatever UIEvent is currently active. 我想以某种方式访问​​任何当前处于活动状态的UIEvent。 Is this possible to do, and if yes, then how? 这可能吗?如果是,那么怎么办?

I solved this by listening for the notification in order to track the most recent UIEvent (I provided the UIEvent in the userInfo). 我通过侦听通知以跟踪最新的UIEvent(我在userInfo中提供了UIEvent)解决了这一问题。 Then, when receiving the notification again, I made sure that it is not the same event as the one that occured just before the custom view appeared (if it is, I skip the removal of the custom view). 然后,当再次收到通知时,我确保该事件与在自定义视图出现之前发生的事件不同(如果是,则跳过自定义视图的删除)。

While this is an answer to my question, the solution to the underlaying problem that @matt proposed in an answer to the following SO question turned out to be much better: How can I know when any of the objects on screen were tapped? 尽管这是我的问题的答案,但@matt在以下SO问题的答案中提出的底层问题的解决方案却要好得多: 我如何知道何时轻按了屏幕上的任何对象?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM