Ios gestures are an indispensable part of ios development. However, you do not know the mechanism of ios gestures and events. So today I would like to share with you the corresponding mechanism of ios events.
First, ios event transmission relies on the "event chain". When an object passed to a link of the chain processes the event, the event will be stopped. So the event chain is a thing of God and horse?
An event chain is an object composed of a group of UIResponder (a base class capable of responding to time). Of course, there are event headers and event tails. Events are transmitted from the beginning, asking one by one whether to process the current event. If it is processed, the transfer will stop.
How to establish the "event chain" of an APP and what is needed to establish it.
Let's first talk about the app structure. Generally, an app is an APPdelegate and an application, then a window is added, rootview is added, and other views are added, such
Figure 1
We can see that the order of app generation is appdegate and UIapplication, and window, and various views. The event chain is also linked based on this structure, forming a tree structure.
We can see red labels and arrows, which are the sequence of event chains. How is the event chain produced? There are two situations.
1. addsubview: Add A normal view B to A. Then, the nextresponder (next responder) of B is assigned A value.
2. If A veiw is not A common view, it is the root veiw of a viewcontroller. In this example, the nextresponder of A is assigned viewControllerA after A is initialized. Then, when A is added to rootview by addsubview, The nextresponder of Controller A is copied as rootview.
The construction of the event chain is not a uniform building in a certain place, But triggered when the addsubview is generated or when the viewController initializes the view. Only two objects are triggered: superview and subview, or viewcontroller of superview and subview.
Since the event chain is a tree structure, events cannot be transmitted between view brothers.
Additionally, the nextresponder of the UIApplication is APPdelete.
Let's talk about how events are transmitted after the event chain is completed.
First, when the user is touched, UIKit first creates an event object and adds it to the event queue. Then, UIAPPlication obtains the object from it, determines the event chain, and then asks for processing in sequence.
Determine the event chain.
There is an hittesting process. As follows:
First, find the window,
1. Call hitTest: withEvent:, hitTest: withEvent: First Call pointInside: withEvent: to determine whether an event occurs in your own region.
2. If YES is returned, the process 1 is called from each subview of the window. Next, call it one by one to find the top view.
Of course, hitTest: withEvent: In addition to pointInside: withEvent:, you can also refer to the hidden attributes and alpha values of view userinteractionenabled and view. When view is hidden or userinteractionenabled is NO, or if alpha is less than 0.01, hitTest: withEvent: NO is returned. For example, view A and view B, such
Figure 2
The superview of A and B first calls hitTest: withEvent: on B, if B is hidden, userinteractionenabled is NO, or alpha is less than 0.01, then B is abandoned, check A instead.
The above is to determine the event chain, and then the transfer is very simple. But with the gesture, it is a little complicated.
First, each of our gestures is actually a series of uievents.
1. Each time one of them is passed, it will be passed from the very beginning of the event chain. First, let's talk about the gesture that the event is passed to the View on the top of the event chain. Then, check whether there is any recognition. If there is no recognition, continue to the gestures of the next responder. If it is recognized immediately, stop the transfer. What should I do if I have passed the entire chain and have not found any other things. Do not think that the event will be lost. The event will return to the front end of the chain, and then find the first view to implement touch (touchbegin, touchend, etc.) for recognition, and then stop this transfer.
2. extract another one from the time and continue the process. Finally, there is no event location.
The above is the entire process of event transfer and recognition.
Parameters of the gesture and special viewdui time transfer are to be processed.