当你设计你的程序的时候,大部分情况你应该动态回应事件。例如,当屏幕上某个对象上有touch事件的时候,你得决定哪个对象回应这个事件,并且明白该对象是怎么接受事件的。
When a user-generated event occurs, UIKit creates an event object containing the information needed to process the event. Then it places the event object in theactive app’s event queue. For touch events, that object is a set of touches packaged in aUIEvent object. For motion events, the event object varies depending on which framework you use and what type of motion event you are interested in.
An event travels along a specific path until it is delivered to an object that can handle it. First, thesingleton UIApplication object takes an event from the top of the queue and dispatches it for handling. Typically, it sends the event to the app’s keywindow object, which passes the event to an initial object for handling.The initial object depends on the type of event:
iOS使用hit-test view去寻找响应touch的view。简单来说就是你触发事件所在的那个View,寻找hit-test view的过程就叫做Hit-Testing。那么,系统是如何来执行Hit-Testing呢?
例子:首先假设现在有如下这么一个UI布局,一种有ABCDE五个View,假设一个单击事件发生在了View D里面,系统首先会从最顶层的View A开始寻找,发现事件是在View A或者其子类里面,那么接着从B和C找,发现事件是在C或者其子类里面,那么接着到C里面找,这时发现事件是在D里面,并且D已经没有子类了,那么hit-test view就是View D啦。
介绍:遍历视图层次结构(traverses the view hierarchy),去寻找接受touch事件的视图。by calling thepointInside:withEvent: method of each subview to determine which subview should receive a touch event. IfpointInside:withEvent: returns YES, then the subview’s hierarchy is similarly traversed until the frontmost view containing the specified point is found. If a view does not contain the point, its branch of the view hierarchy is ignored.You rarely need to call this method yourself, but you might override it to hide touch events from subviews。
详细执行过程:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ if ([self pointInside:point withEvent:event]) { for (UIView *view in [self subviews]) { UIView *result = [view hitTest:point withEvent:event]; if (result != nil) { return result; } } return self; }else{ return nil; } } - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{ return YES; }
说明:
1.该方法不起作用的情况(相当返回nil):userInteractionEnabled = NO 、hidden = YES 、alpha<0.01 (跟视图内容无关,例如内容透明等)。
2.hit-test view只是快速找到第一响应者,首先得到了处理a touch event的机会,If the hit-test view cannot handle an event(或者hit-test没有找到第一响应者), the event travels up that view’s chain of responders as described in<The Responder Chain Is Made Up of Responder Objects> until the system finds an object that can handle it。
Many types of events rely on a responder chain for event delivery. Theresponder chain is a series of linked responder objects. It starts with the first responder and ends with the application object. If the first responder cannot handle an event, it forwards the event to the next responder in the responder chain.
A responder object is an object that can respond to and handle events. TheUIResponder class is the base class for all responder objects, and it defines the programmatic interface not only for event handling but also for common responder behavior. Instances of theUIApplication, UIViewController, and UIView classes are responders, which means that all views and most key controller objects are responders. Note that Core Animation layers are not responders.
The first responder is designated to receive events first. Typically, the first responder is a view object. An object becomes the first responder by doing two things:
1.Overriding the canBecomeFirstResponder method to return YES.
2.Receiving a becomeFirstResponder message. If necessary, an object can send itself this message.
// 注意: Make sure that your app has established its object graph before assigning an object to be the first responder. For example, you typically call the becomeFirstResponder method in an override of the viewDidAppear: method. If you try to assign the first responder in viewWillAppear:, your object graph is not yet established, so the becomeFirstResponder method returns NO.
Events are not the only objects that rely on the responder chain. The responder chain is used in all of the following:
If the initial object—either the hit-test view or the first responder—doesn’t handle an event, UIKit passes the event to thenext 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.
The responder chain sequence begins when iOS detects an event and passes it to an initial object, which is typically a view. The initial view has the first opportunity to handle an event. Figure 2-2 shows two different event delivery paths for two app configurations. An app’s event delivery path depends on its specific construction, but all event delivery paths adhere to the same heuristics.
For the app on the left, the event follows this path:
//注意: 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