Touch Events in iOS

Source: Internet
Author: User

Overall, there are 2 steps:

One, find the right control from top to bottom to handle this touch event. For example, if you click Yellow 4, then uiapplication, UIWindow, 1 white, 2 orange, 3 blue and 4 yellow.

Second, find 4 yellow, then from bottom to top traverse the responder chain: 4 yellow, 3 blue, 2 orange, 1 white, UIWindow, uiapplication

1) If 4 yellow realizes the touches ... These functions (specifically the second one below) and do not call super ... The event is no longer passed up, if super ... is called. Method, the event continues to be passed up.

2) If 4 yellow is not implemented touches ... These functions are passed straight up.

Detailed Description:

1 What is a responder object

Not all objects in iOS can handle events, only objects that inherit Uiresponder can receive and handle events. We call it "responder object."

2 common handling methods for touch events

    • -(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event; one or more fingers begin to touch the view
    • -(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event, one or more fingers moving on the view (only if a certain displacement is generated)
    • -(void) touchesended: (Nsset *) touches withevent: (Uievent *) event; one or more fingers leave the view, the system automatically calls the following method of view
    • -(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) event; A system event (such as a phone call) interrupts the touch process before the touch is finished

Tip: Uitouch objects are stored in the touches

3 Uitouch Objects

When the user touches the screen with a finger, a Uitouch object is created that is associated with the finger, and a finger corresponds to a Uitouch object. If there are two fingers there will be Uitouch objects, so you can determine the number of fingers (Nsset *) According to how many touches.

Role:

    • Keep the information related to the finger, such as the position, time and stage of the touch;
    • When the finger moves, the system updates the same Uitouch object;
    • When the finger leaves the screen, the system destroys the corresponding Uitouch object.

4 UIView do not receive three cases of touch events

    • userinteractionenabled = NO
    • Hidden = YES
    • Alpha = 0.0 ~ 0.01

Tip: Uiimageview's userinteractionenabled default is no, so it and its child controls cannot receive touch events by default.

5 Finding the right responder

Find the right controls from top to bottom, such as the parent control child control, UIApplication-I, UIWindow, to find a level down.

Look for the order, or the principle

    • 1> can I receive touch events? No, the event is passed to this end
    • 2> touch points on their own? No, the event is passed to this end. Call
    • 3> traverse the child control from the back, repeating the previous two steps
    • 4> If there are no child controls that meet the criteria, then you are best suited to handle

Two functions called:-(UIView *) HitTest: (cgpoint) point withevent: (Uievent *) event;-(BOOL) Pointinside: (cgpoint) point withevent: ( Uievent *) Event

Summary: It is clear that child controls are no longer found if the parent control cannot receive it.

6 Responder Chain

Find the right control from the bottom up to handle this event, if the touches is implemented ... function without calling the Super method, the event is no longer uploaded, and if no touches is implemented or the Super method is called in touches, the event continues to upload. (Super does not refer to the parent class, but to the previous responder)

Tip: If the current view is a view of the controller, the controller is the upper-level responder, otherwise the upper-level responder is the parent control.

7 self-Realization touches ...

If you do not call super ..., the system does not do the default processing, such as implementing the UIButton Touchbegan method, the system does not perform a button's strike event (no message is sent).

Touch Events in iOS

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.