Response to Events (Uiresponder), Touch, Uigesturerecognizer

Source: Internet
Author: User

The processing interface for events in iOS or the protocol is defined by Uiresponder.

The most important event is the touch event, which is detected and encapsulated by the Uiaapplacation object Runloop. The Uiapplaction object is encapsulated to pass the event to the Uiwindeow object. Touch events are handled in two steps, the first step is to find the corresponding view of the touch, that is, the touch is that on the view, and the second step is to find the object that handles the event.

Implementation of the first step, directly call UIWindow's None

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

method, which returns the view that corresponds to the touch event in the view tree that is the root of the caller. The implementation of this method is to invoke the none of all child view

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

And then returns YES to the child view call None

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

This recursive call until there is no leaf view, and then returns the view. Note that this recursive invocation of the point value of each pass is a virtual transition. Position in the corresponding view coordinate system.

What needs to be explained is the uiinterfaceenable of the view and the presence of gesture recognize images of the process. If the uiinterfaceenable is no, and the canceltouchinview of the gesture recognize is yes, then the process will not go on. If Uiinterfaceenable is no, super view does not hittest such a view. Gesture recognize's Canceltouchinview view does not hittest the child view.

The second step is to find the object that handles the event. The view that you find from the first step starts passing the event along the Reponder chain. Note that an object in responder chain can not continue to pass the event down, then responder chain the objects below will not process the object. The common chain are view–> viewcontroller–>super view–> superview controller-> Super Super View-> Supersupervi EW Controller finally to Rootcontroller and then to window. It should be noted that the next responder of Viewcontroller is Superview. iOS If the view Congroller is not used in accordance with the Tabbar Navigationbar Viewcongroller method, it may produce responder chain the wrong situation. (Direct view Congroller view Addsubiew to other view)

From the above we can see that an event allows multiple objects to be known and processed at the same time. The interface for this chain implementation is none.

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event;

-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event;

-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event;

-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) event;

The default implementation of the four methods is the four methods that invoke Nextresponder. That is to say, if you do not write these methods, the event will eventually pass to the Window,window processing is generally discarded directly.

Let's say Uigesturerecognizer:

The Uigesturerecognizer object is added to the View object, and the Gesturerecognizer is notified when the view receives HitTest. Gesturerecognizer also has and encapsulates the above four methods, and then carries out specific actions based on the time position characteristics of the touch events. The iOS system provides a number of Uigesturerecognizer subclasses that deal with specific touch sequences.

Uigesturerecognizer provides a number of proxies to conveniently encapsulate the implementation of the above four methods. The following method defines whether a touch event is added to the sequence of events according to the situation, generating the action of the response. None

-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) touch;

In addition to the handling of other events for touch events, the first responders need to be introduced, including the none Motion Events-via calls to the Uiresponder motion-handling methods described I N "shaking-motion Events" Remote-control Events-via calls to the Uiresponder method Remotecontrolreceivedwithevent:actio n messages-sent when the user manipulates a control (such as a button or slider) and no target are specified for the action Message Editing-menu messages-sent When users tap the commands of the Editing menu (described in "displaying and managing The Edit menu)

There is only one first responder in the program. The first responder-related function is None

-(BOOL) Canbecomefirstresponder; Default is NO

-(BOOL) Becomefirstresponder;

-(BOOL) Canresignfirstresponder; Default is YES

-(BOOL) Resignfirstresponder;

-(BOOL) Isfirstresponder;

Object through-(BOOL) Becomefirstresponder; To register yourself (or any other object) as the first responder to the program.    The registered person passes-(BOOL) Canbecomefirstresponder method to indicate whether he is the cause of the first responder. After the event that requires the first responder arrives, the event is handed over to the first responder, then the first responder passes the event along the responder chain. The responder chain of the motion event is by None–motionbegan:withevent:–motionended:withevent:–motioncancelled:withevent:

Method is implemented in the same way as the Touch responder chain method, and if no object is processed, it is passed to the UIApplication object and discarded.

(Transfer from: http://dcm19872007.blog.163.com/blog/static/865193742013018112558478/)

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.