iOS文档--Using Responders and the Responder Chain to Handle Events

Article

Using Responders and the Responder Chain to Handle Events

Learn how to handle events that propagate through your app.

       --了解如何处理通过app传播的事件。

 

Overview

Apps receive and handle events using responder objects. ★ A responder object is any instance of the UIResponder class, and common subclasses include UIView, UIViewController, and UIApplication. Responders receive the raw event data and must either handle the event or forward it to another responder object. When your app receives an event, UIKit automatically directs that event to the most appropriate responder object, known as the first responder.

         --app是使用responder objects.来处理事件的。 响应者接收原始事件数据后,要么处理该事件,要么就将事件转发到另一个响应者。当app接收到事件后,UIKit会自动将该事件导向到最合适的响应者中(称为第一响应者)。这个★  自动导向过程你是无法操作的,但是你可以从第一响应者中处理事件,或者继续传递,所以,你操作事件的起点是第一响应者,而不是UIAppication。你的起点是UIKit导向的终点,然后再往回传播,★  往回传播的过程你是可以操作的。(自动导向过程要用UIVIew的hittest方法)

Unhandled events are passed from responder to responder in the active responder chain, which is the dynamic configuration of your app’s responder objects. Figure 1 shows the responders in an app whose interface contains a label, a text field, a button, and two background views. The diagram also shows how events move from one responder to the next, following the responder chain.

            --未处理的事件在“活动响应者链”上从响应者传递到响应者,这就是app上的响应者对象的动态配置。下图展示了事件是如何在响应者链上的响应者中传递的。

Figure 1

Responder chains in an app

A flow diagram: On the left, a sample app contains a label (UILabel), a text field for the user to input text (UITextField), and a button (UIButton) to  press after entering text in the field. On the right, the flow diagram shows how, after the user pressed the button, the event moves through the responder chain—from UIView, to UIViewController, to UIWindow, UIApplication, and finally to UIApplicationDelegate.

If the text field does not handle an event, UIKit sends the event to the text field’s parent UIView object, followed by the root view of the window. From the root view, the responder chain diverts to the owning view controller before directing the event to the window. If the window cannot handle the event, UIKit delivers the event to the UIApplication object, and possibly to the app delegate if that object is an instance of UIResponder and not already part of the responder chain.

         --★  如果UITextField不处理事件,UIKit就将事件发送到UITextField的父UIView对象中,然后是window的根view中。在根view传递事件给window之前,UIKit会先将事件传递给根view的vc上。如果window无法处理该事件,则UIKit会将该事件传递给UIApplication对象,如果该对象是UIResponder的实例且不属于响应程序链的一部分,则可能会传递给app委托。

 

Determining an Event's First Responder         --任命事件的第一响应者的默认设置

UIKit designates an object as the first responder to an event based on the type of that event. Event types include:

       --UIKit是根据event的类型来确定第一响应者的。

Event type

First responder

Touch events

The view in which the touch occurred.

Press events

The object that has focus.

Shake-motion events

The object that you (or UIKit) designate.

Remote-control events

The object that you (or UIKit) designate.

Editing menu messages

The object that you (or UIKit) designate.

 

Note

Motion events related to the accelerometers, gyroscopes, and magnetometer do not follow the responder chain. Instead, Core Motion delivers those events directly to the designated object. For more information, see Core Motion Framework

         --运动事件的的响应是与硬件传感器相关的,而不是通过响应者链,具体参考超链接。

Controls communicate directly with their associated target object using action messages. When the user interacts with a control, the control sends an action message to its target object. Action messages are not events, but they may still take advantage of the responder chain. When the target object of a control is nil, UIKit starts from the target object and traverses the responder chain until it finds an object that implements the appropriate action method. For example, the UIKit editing menu uses this behavior to search for responder objects that implement methods with names like cut(_:), copy(_:), or paste(_:).

        --控件使用动作消息直接与其关联的目标对象通信。当用户与控件交互时,控件就向它的目标对象发送动作消息。动作消息不是事件,但它们也是利用“响应者链”进行传播的。当控件的目标对象为nil时,UIKit从目标对象开始并遍历响应者链,直到找到愿意处理该消息的动作方法。例如,UIKit编辑菜单使用此行为来搜索响应者对象,这些对象实现了名为cut(:)、copy(:)或paste(:)的动作方法。

Gesture recognizers receive touch and press events before their view does. If a view's gesture recognizers fail to recognize a sequence of touches, UIKit sends the touches to the view. If the view does not handle the touches, UIKit passes them up the responder chain. For more information about using gesture recognizer’s to handle events, see Handling UIKit Gestures.

        --★  手势识别器会在“视图之前“接收到触摸和按下事件。如果视图的手势识别器无法识别一系列触摸,UIKit会将触摸发送到视图。如果视图不能处理触摸,UIKit会将它们传递到响应者链上。有关使用手势识别器处理事件的更多信息,请参阅处理UIKit手势。

 

Determining Which Responder Contained a Touch Event   -- 指定哪一个响应者拥有触摸事件

UIKit uses view-based hit-testing to determine where touch events occur. Specifically, UIKit compares the touch location to the bounds of view objects in the view hierarchy. The hitTest(_:with:) method of UIView traverses the view hierarchy, looking for the deepest subview that contains the specified touch, which becomes the first responder for the touch event.

           --UIKit使用基于view的hit-testing来确定触摸事件发生的位置。具体来说,UIKit将触摸位置与视图层次结构中view对象的bounds进行比较。UIView的hitTest(:with:)方法遍历视图层次结构,查找包含改触摸事件的最深子视图,该子视图将成为触摸事件的第一个响应者。

Note

If a touch location is outside of a view’s bounds, the hitTest(_:with:) method ignores that view and all of its subviews. As a result, when a view’s clipsToBounds property is false, subviews outside of that view’s bounds are not returned even if they happen to contain the touch. For more information about the hit-testing behavior, see the discussion of the hitTest(_:with:) method in UIView.

         --如果触摸位置超出view的bounds范围,那么hitTest(:with:)方法将忽略该视图及其所有子视图。因此,当视图的clipsToBounds属性为false时,该view的边界之外的子视图将不会返回,即使它们碰巧包含touch。有关hit-testing行为的更多信息,请参阅UIView中对hitTest(:with:)方法的讨论。

When a touch occurs, UIKit creates a UITouch object and associates it with a view. As the touch location or other parameters change, UIKit updates the same UITouch object with the new information. The only property that does not change is the view. (Even when the touch location moves outside the original view, the value in the touch’s view property does not change.) When the touch ends, UIKit releases the UITouch object.

         --当触摸发生时,UIKit会创建UITouch对象并将其与view关联。随着触摸位置或其他参数的更改,UIKit会使用新信息更新该UITouch对象。唯一不变的属性是视图。(即使触摸位置移动到原始视图之外,触摸的视图属性中的值也不会更改。)触摸结束时,UIKit会释放UITouch对象。

 

Altering the Responder Chain        -- 更改响应者链中的响应者

You can alter the responder chain by overriding the next property of your responder objects. When you do this, the next responder is the object that you return.

         --你可以复写响应者的next property来指定下一位响应者。

Many UIKit classes already override this property and return specific objects, including:

      --许多UIKit类已经覆盖了这个属性并返回特定的对象,例如:

  • UIView objects. If the view is the root view of a view controller, the next responder is the view controller; otherwise, the next responder is the view’s superview.
         --UIView 对象,有vc的话,则下一位响应者就是vc,否则就是父view。

  • UIViewController objects.

    • If the view controller’s view is the root view of a window, the next responder is the window object.

    • If the view controller was presented by another view controller, the next responder is the presenting view controller.
      --如果vc是被其他vc呈现的,那么下一个响应者就是正在呈现的vc

  • UIWindow objects. The window's next responder is the UIApplication object.

  • UIApplication object. The next responder is the app delegate, but only if the app delegate is an instance of UIResponder and is not a view, view controller, or the app object itself.
            --UIApplication object的下一位响应者是app delegate

 

See Also

First Steps

class UIResponder

An abstract interface for responding to and handling events.

class UIEvent

An object that describes a single user interaction with your app.

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值