IOS Development Touch Event _ios

Source: Internet
Author: User
Tags touch

I. Event distribution processing "from outside to Inside"

When a touch occurs in iOS, the event is added to the UIApplication event queue, and UIApplication takes the first event out of the event queue for distribution, which is usually distributed to the main window and the main window invokes the Hittest:withevent: method, Find the appropriate event trigger view, that is, to find the touched View object

The search process is as follows:

    1. Call Pointinside:withevent on the top-level view (Keywindow view): method to determine whether the touch point is in the current view;
    2. If no is returned, then the Keywindow hittest:withevent: return to nil;
    3. If yes is returned, it sends Hittest:withevent to all child views of the current view: message, the order in which all the child views are traversed is traversed forward from the end of the Subviews array (from the top of the interface);
    4. If there is a subview hittest:withevent: Returns a Non-empty object, then the hittest:withevent of Keywindow: Returns this object, processing ends;
    5. If all subview traversal ends still do not return Non-empty objects, then Keywindow's hittest:withevent: Returns to the top-level view;

Second, the responder chain "from the Inside Out"

Once you find the touched View object, you also need to determine whether the view object can handle the touch event, and if not, who should handle it, so the responder chain appears to find the event responder

Responder Chain principle:

    1. When a touch object Initalview cannot respond to an event, it is passed to the superior view Superview to respond
    2. If the superior view is not responding, continue to pass on
    3. Passed up until the root view of the view controller is passed to Controllerview, and if the root view does not respond, it is passed to the view controller Viewcontroller
    4. The view controller is not responsive and is passed to the parent view controller's root view Supercontrollerview and passed to the parent view controller if the root view does not respond Superviewcontroller
    5. The top-level view controller cannot respond and is passed to the main window Keywindow
    6. Keywindow can not respond, passed to uiapplication processing
    7. UIApplication cannot respond, the event is discarded

Iii. Inheritance of Uiresponder

The above event distribution and responder chains do not require our attention, these actions are performed automatically, we do not need to operate, we just need to understand their principles.
Not all classes in iOS can handle and accept events, and only objects that inherit Uiresponder can handle events (our common UIView, Uiviewcontroller, uiapplication inherit from Uiresponder, They can both receive and process events), but inheriting uiresponder does not necessarily handle events

An object that inherits Uiresponder and cannot handle the case of an event:

Copy Code code as follows:

userinteractionenabled = NO;
hidden = YES;
Alpha = 0 ~ 0.01;

No Touchesbegan:withevent method implemented

Overriding the Uiresponder Touch response method:

Copy Code code as follows:

#pragma mark Touch starts calling
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event;
#pragma mark Touch moves frequently
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event;
#pragma mark touch ends when he leaves the screen.
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event;
#pragma mark will call when he touches an unexpected cancellation, such as when touching a phone call.
-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) event;

Iv. Uitouch Objects

In the Uiresponder touch response method, you need to get the Uitouch object:

Copy Code code as follows:
Get a Touch object (there may be multiple objects for multi-touch)
Uitouch *touch = [touches anyobject];
Gets the touch position in the specified view
Cgpoint current = [Touch LocationInView:self.view];
Gets the previous touch position in the specified view
Cgpoint previous = [Touch PreviousLocationInView:self.view];

Other Common properties:

Windows: Touch the Window
View: Touch the views
Tapcount: Click times in short time

Knowing this, you can do some fun things with touch events, (^o^)/~, there's no specific code here.

Related Article

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.