iOS Development event handling: Three: Finding the most appropriate view

Source: Internet
Author: User

1: The generation and delivery of events:

2: Find the most appropriate view: How to find the most appropriate view: Three steps: 1: First to determine whether you can accept Touch Event 2: Determine if the touch point is on its own 3: If the first two are satisfied, then it will traverse all the child controls from the back forward (from the backward forward to take into account the problem of efficiency, Refers to the last control in the array of child controls) continue repeating 12 steps until the most appropriate child control is found. (A control cannot accept three conditions for a touch event, which is passed by the parent control to the child control, and if the parent control cannot accept the event, the child control must not accept the event)

:

Note: Traversing a child control from the back: always iterates through the last of the child control arrays, which is the last child control added to the parent view

Add order of child controls in: 1: First add a Red control view, add a Blue control view, then add yellow, green, and finally add purple

For example, when a green view is clicked, the system sends the event to the Uiapplecation managed event queue, and Uiapplecation takes the most front-end event from the event queue to the main window Keywindow, At this time Keywindow do judge oneself can accept touch event, Touch point in own body, meet two conditions, at this time Keywindow will look for there is no more suitable view, at this time Keywindow will traverse the child control from the back and forth, Get the child control after the white view to continue to determine whether to meet two conditions, not satisfied then the event will not continue to pass, if satisfied, then the white view will traverse the child control from the back, first look at the white control of the child control array of the last is the blue control, in view of the purple view, viewing the yellow , and then look at the green view, whether the condition is satisfied, not satisfied then stop the delivery or continue to pass to the child control, stop delivery is related to the problem of user interaction, if the child control can not receive events, then this is the parent view is the most appropriate view, The parent view handles this event (Viewui, the topmost is UIWindow, remembering that the most important thing is that the delivery of the event is passed by the parent control to the child control, the parent control cannot accept the event, the child control cannot receive the event, and the control cannot accept the three conditions of the touch event)

3: The lowest method of the event pass call: 1: When the view receives a touch event, it is called,-(UIView *) HitTest: (cgpoint) point withevent: (uievent *) The event method is called at the bottom,-(BOOL) Pointinside: (cgpoint) point withevent: (Uievent *) event. The second method is called first when the first method is called. 2: Generally if you want to go back to any of the view processing click events, you generally consider overriding the two methods in the parent view, returning the view of the child control in the first method

Role: To find the most suitable view

When to invoke: When an event is passed to the current view, it is called.

Return value: Who is returned, who is the most appropriate view (will call the most appropriate touch method of the view)

-(UIView *) HitTest: (cgpoint) point withevent: (Uievent *) event{

NSLog (@ "%s", __func__);

UIView *fitview = [Super Hittest:point withevent:event];

NSLog (@ "%@", Fitview);

return self.subviews[0];

return fitview;

}

Function: Judge the current point in the not it call view, (who calls Pointinside, this view is Who)

When to call: it is called in the HitTest method.

Note: Point points have to be in the same coordinate system as the method caller.

-(BOOL) Pointinside: (cgpoint) point withevent: (Uievent *) event{

NSLog (@ "%s", __func__);

return NO;

}

iOS Development event handling: Three: Finding the most appropriate view

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.