The principles of event handling for iOS learning

Source: Internet
Author: User

in the iOS Learning 23 event processing, the small part of the detailed introduction of event processing, here is a small description of its related principles

1. Uitouch Object

There is a collection of Uitouch objects in the handling of touch events, what is the use of this parameter?

(1) Introduction to Uitouch objects
    • When the user touches the screen with one finger, a Uitouch object associated with the finger is created

    • One finger corresponds to a Uitouch object

    • The role of Uitouch

Holds information about a finger, such as the location, time, and stage of a touch

    • When the finger moves, the same Uitouch object is updated to keep the finger in the touch position

    • When the finger leaves the screen, the system destroys the corresponding Uitouch object

 Tip: To avoid using double-click events in IPhone development!

(2) Properties of Uitouch
    • The window generated when the touch is generated
ReadOnly, retain) UIWindow *window;
    • The view generated when the touch is generated
ReadOnly, retain) UIView *view;
    • The number of times you tap the screen in a short time can be judged by Tapcount click, double tap or more
ReadOnly
    • Records the time when a touch event is generated or changed, in seconds
readonly) Nstimeinterval timestamp;
    • The state of the current touch event
readonly) Uitouchphase phase;
(3) Methods of Uitouch
-(Cgpoint) Locationinview: (UIView *) view;  // This method records the position of the previous touch point -(Cgpoint) Previouslocationinview: (UIView *) view;
    • The position returned in method one is for the view's coordinate system (the origin point (0,0) in the upper-left corner of the view)

    • When the view parameter is nil when the method is called, it returns the position of the touch point in the UIWindow.

Instance code:

//when the finger moves on the view- (void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{NSLog (@"%s", __func__); //Get Uitouch ObjectUitouch *touch =[touches anyobject]; //gets the current pointCgpoint curp =[Touch locationinview:self]; //Get previous pointCgpoint PreP =[Touch previouslocationinview:self]; //get x-axis offsetCGFloat OffsetX = curp.x-prep.x; //Get y-axis offsetCGFloat OffsetY = curp.y-prep.y; //Modify the view's location (Frame,center,transform)Self.transform =cgaffinetransformtranslate (Self.transform, OffsetX, OffsetY);//self.transform = cgaffinetransformmaketranslation (OffsetX, 0);}

The default does not support multiple fingers, to modify the multiple Touch property, in the storyboard this property hook can be

2. Principle(1) generation and transmission of events
    • After a touch event occurs, the system adds the event to an event queue managed by UIApplication

    • UIApplication takes the first event from the event queue and distributes the event for processing, typically sending an event to the application's main window (Keywindow)

    • The main window finds the most appropriate view in the view hierarchy to handle touch events, which is the first step in the entire event processing process.

    • Once the appropriate view control is found, the touches method of the view control is called to do the specific event handling

Touchesbegan ...

Touchesmoved ...

Touchesended ...

(2) UIView does not receive three cases of touch events
    • User interaction Shutdown
userinteractionenabled = NO;
    • View Hidden
hidden = YES;
    • Transparent
0.0 0.1
(3) How to find the most appropriate control
    • To determine if you can receive touch events, the underlying implementation method: Hittest:withevent:
// Purpose: To find the most suitable view // When to invoke: When the event is passed to the control, it invokes the control's method to find the most appropriate view // Point : The current touch point, which is the coordinate system of the method caller -(UIView *) HitTest: (cgpoint) points withevent: (uievent *)event {    //  Call the system to find the most appropriate view, return to the most appropriate view    UIView *fitview = [Super Hittest:point Withevent:event];     return Fitview;}

The return value of the method is the view control for event handling

When the method is overridden in the UIWindow class, it returns SELF.SUBVIEWS[0], regardless of the point where the bottom-most white view is processed.

    • Determine if the touch point is on its own, the underlying implementation method: Pointinside:withevent:

// function: Determines whether the current point is not on the method caller (control) // called when determining if the control is the most appropriate -(BOOL) Pointinside: (cgpoint) point withevent: (uievent *)event{     return YES;}

    • Iterate through the child controls backwards, repeating the previous two steps

The principles of event handling for iOS learning

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.