iOS Development event handling: Second: The generation and delivery of events

Source: Internet
Author: User

1. How are events generated and delivered?

when a touch event occurs, the system adds the event to an event queue managed by UIApplication. (Queue is FIFO, while stack is advanced)

UIApplication takes the first event out of the event queue and distributes the event for processing. First to the main window Keywindow to deal with

The main window finds the most appropriate view in the view hierarchy to handle the touch event , and when the most appropriate view is found to handle the event, the view calls the Touchbegan and other methods to process the event

2: The sequencing of time delivery and the points of attention:

The delivery of a touch event is passed from the parent control to the child control.

If a parent control cannot receive an event, the child controls inside it are also not able to receive events.

3: A control is not able to receive events under what circumstances :

1. Cannot handle an event when no user interaction is received : If the parent control cannot interact with the user, then the child control does not receive a touch event, and the touch event is then passed to the parent control's parent control to handle

userinteractionenabled = NO

2. When a control is hidden , it cannot receive events: When a parent control is hidden, the child controls on it are hidden, that is, when the parent control cannot accept touch events, the child controls on it cannot accept touch events

Hidden = yes at the time

3. When a control is transparent, it cannot receive events : When a parent control is transparent, the child controls are also transparent, and when the opacity is between 0.0 and 0.01, the control cannot accept touch events (when transparency is 0, it is completely transparent, 0.0- 0.01 contains both 0.0 and 0.01 values)

Note: Uiimageview's userinteractionenabled default is no,

So Uiimageview and its child controls are not allowed to receive touch events by default

4: uievent: Each occurrence of an event produces a Uievent object. Uitouch is a touch object, a finger touch produces a Uitouch object, and two finger touches produce two Uitouch objects. Uievent also provides a way to get a touch object on a view Uitouch

5: Example of event delivery:

1: The order in which events are delivered

2: Create four different colors of view, the implementation of the Touchbegan method in four view, 1: Four view has the same code, then the parent class directly implement the Touchbegan method in the parent class, let the subclass inherit, how to distinguish the different subclasses in the parent class, You can see the class that the subclass belongs to by viewing the Self.class in the parent class, or the parent class exposes the type interface, and the subclass overrides the method of the parent class, and the parent class can judge different subclasses based on the type of the assignment, allowing different subclasses to implement different methods in the methods of the parent class. When the subclass inherits the parent class, it has all the code of the parent class, and if you want the child class and the parent class to be associated for data passing, the parent class can expose the method for subclasses to rewrite, 1: Subclasses can override the parent class's method to get something different from the parent class. 2: The parent class exposes the interface settings return value, The parent class can get the return value in the method overridden by the subclass (Get Method Interface) 2: If you first create a xib file and want to encapsulate a control in Xib, you can customize the class in the Xib, inherit the class from the system, and then come to Xib and associate the class with the control you want to encapsulate:

1:1: When the yellow view is set to userinteractionenabled = NO, when the yellow view is clicked, the event is passed by the parent control to the child control, and if the parent control cannot accept the event, The child control is also unable to accept the event (3 conditions make the control unable to accept the Click event), the event is orange-blue, and the view cannot interact with the user, so the event cannot be passed to the yellow view, only to the Blue view, Blue View can handle the event, It will call its Touchbegan Method 2: When setting the blue view of userinteractionenabled = NO, click on the yellow view, the event will only be passed to the orange view, blue cannot accept touch events, So the event will not reach the blue view, and it will not spread to the yellow view. So the event is handled by the Orange view.

2:1: uiimageview default userinteractionenabled = NO, Uiimageview and child controls on it cannot accept touch events, By default in Xib, you cannot add child controls on Uiimageview, and you can add 2 through code: There is also the setting of transparency, With transparency between 0.0-0.01, the parent control transparent child control is also transparent, the parent control is hidden, and the child control is hidden, and neither its parent nor child controls accept the touch event

iOS Development event handling: Second: The generation and delivery of events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.