ios - 当它说 "an event couldn' t 被处理时是什么意思?

标签 ios swift2 hittest uiresponder uievent

我正在阅读 UIResponder 类的文档,并且遇到了响应者链。现在据说,例如,当一个 HitTest 无法处理一个事件时,它会将它传递给一个响应者链。那么,有什么例子可以阐明如何处理事件?

最佳答案

来自 Event Handling Guide for iOS

If the initial object—either the hit-test view or the first responder—doesn’t handle an event, UIKit passes the event to the next responder in the chain. Each responder decides whether it wants to handle the event or pass it along to its own next responder by calling the nextResponder method. This process continues until a responder object either handles the event or there are no more responders.


“处理”事件的含义实际上取决于每个响应者类。实现响应者时需要做出的决定是,对于每个事件,是否将其传递给下一个响应者。
该部分的底部还有一个重要说明:

Important: If you implement a custom view to handle remote control events, action messages, shake-motion events with UIKit, or editing-menu messages, don’t forward the event or message to nextResponder directly to send it up the responder chain. Instead, invoke the superclass implementation of the current event handling method and let UIKit handle the traversal of the responder chain for you.


同样,对于最常见的情况,您的响应者是 UIView。处理触摸事件的子类,所有这些方法包括:

The default implementation of this method does nothing. However immediate UIKit subclasses of UIResponder, particularly UIView, forward the message up the responder chain. To forward the message to the next responder, send the message to super (the superclass implementation); do not send the message directly to the next responder. For example,

[super touchesBegan:touches withEvent:event];

If you override this method without calling super (a common use pattern), you must also override the other methods for handling touch events, if only as stub (empty) implementations.

关于ios - 当它说 "an event couldn' t 被处理时是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38546443/

相关文章:

ios - FBSDKAccessToken.currentAccessToken() 每次都发现 nil

ios - 如何将预填充的 Default.realm 文件放到设备上?

objective-c - iPhone的简单动画

ios - SKStoreReviewController requestReview 对话框中缺少文本

ios - 分组 TableView - 新条目需要新部分。使用核心数据

ios - 如何调整图像大小以适应包含的 ImageView

swift - 按下按钮(快速代码)时如何更改为约束前后?

ios - 如何获取另一个 uiwindow 之外的 uibutton 的触摸事件

javascript - Html5 Canvas HitTest 门的任意形状

iphone - 正确处理多个 View 的触摸