1. Diagram of the process of iOS event processing
1. When the screen is touched, the response message is captured by the operating system, and the message is placed into the UIApplication managed queue.
2. Take the message from the uiapplication and give it to Uiwindow,uiwindow to make the following judgments:
- Determine if you can receive events
- Dot is not on his own window
- Traverse your own child controls to find a more appropriate view than yourself
- The child control receives, continues the above steps; the child control does not receive, the window handles the event itself
3. Until the most appropriate Yellowview is found, the event is delivered at the end, and then the responder chain is executed in reverse.
4.touch Default Practice: Do not process yourself, to the previous responder. The previous responder defaults to the parent control. You can override the touch method to handle a response click event, overriding the method if the parent class's method is not called, the responder chain ends, and the call continues to go down.
2. Detailed Hittest:withevent: Method 2.1 Hittest:withevent: The Role of
-(UIView ) HitTest: (cgpoint) point withevent: (Uievent ) Event
1. Hittext When to call: When an event is passed to a control, the control calls this method
2. Hittext role: Find the most appropriate view.
3. Event delivery, UIApplication-UIWindow. UIWindow to find the right view? [UIWindow hittest:withevent:] What's going on inside?
- Can I tell if the window will handle events? If not, it means that the window is not the most suitable view, and will not look for a more appropriate view than yourself, directly return nil, notify UIApplication, there is no most appropriate view.
- Judging points are not in the window
- Traverse your own child controls to find out if there is a more appropriate view than yourself
- If the child control does not receive an event, it means that the child control does not find the most appropriate view, and then returns nil, telling the window not to find a more appropriate view, and the window knows that there is no more appropriate view for itself to handle the event.
2.2 Implementation Hittest:withevent:
Implementation ideas:
1. Determine if you can receive events
2. Judging points are not on their own window
3. Traverse your own child controls to find a more appropriate view than yourself
//point: Is the location of the touch points on the method caller's coordinate system- (UIView*) HitTest: (Cgpoint) Point withevent: (Uievent *) event{//1. Determine if the touch event can be received if( Self. userinteractionenabled==NO|| Self. Hidden==YES|| Self. Alpha<=0.0)return Nil;//2. Determine if the next point is not on the control if([ SelfPointinside:point Withevent:event] = =NO)return Nil;//3. Traverse the child control from back to forward intCount = (int) Self. Subviews. Count; for(inti = count-1; I >=0; i--) {//Remove the child controls that appear at the front UIView*childview = Self. Subviews[i];//Convert to a sub-control point on the coordinate system CgpointCHILDP = [ SelfConvertpoint:point Toview:childview];UIView*fitview = [Childview hittest:childp withevent:event];if(Fitview) {returnFitview; } }//means there is no more suitable view than yourself return Self;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Event handling in iOS-sequel