Event Distribution and responder chain

Source: Internet
Author: User

In iOS, events fall into three categories:

    1. Touch events: Trigger by Touch, gesture (click, zoom, etc.)
    2. Motion events: Triggering via an accelerator (shaking, etc.)
    3. Remote control event: triggered by other remote device (line-controlled headset)

Only classes that inherit from Uiresponder can handle events

Here's a touch event.

When the user touches the screen, the event is encapsulated into an event instance, which contains the touch-related information, which is then distributed by the operating system and processed by the responder class (Uiresponder subclass);

Simple distribution and response process diagram:

1. Distribution of events

When the iOS system detects a finger touch operation, it puts it into the event queue of the currently active application, UIApplication takes the touch event from the event queue and passes it to the key window (which is currently receiving user events) for processing. The Window object first uses the Hittest:withevent: method to find the view that contains the initial point of the touch operation-the view that needs to pass the touch event to its processing, called Hit-test view.

The Window object will be hittest:withevent first in the top view of view hierarchy: This method calls Pointinside:withevent on each view in the view hierarchy:, If pointinside:withevent: Returns Yes, continue the cascade call until you find the location where the touch operation occurred, which is hit-test view.

Hittest:withevent: The process of the method is as follows:

  1. First call the current view's Pointinside:withevent: method to determine whether the touch point is within the current view;
  2. If no is returned, hittest:withevent: returns nil;
  3. If yes, the Hittest:withevent: message is sent to all the child views of the current view (Subviews), and all child views are traversed from top to bottom, that is, from the end of the subviews array. Until a child view returns a non-empty object or all the child views are traversed;
  4. If the child view returns a non-empty object for the first time, the Hittest:withevent: method returns this object, processing ends;
  5. If all child views return non, then Hittest:withevent: method returns itself (self)

Simply put, through Hittest:withevent: and Pointinside:withevent: These two methods find the Touch object ( Touch object is not necessarily the responder )

2. Responder Chain

In a nutshell, the responder chain is a chain of instance objects that have the current event-handling power (as the understanding is wrong, please note)

Object on the responder chain 1. Handling events (after processing an event will not continue to be passed to the next responder, you can pass the current event to an object for processing by means of a replication method)

2. Do not process (if all responders do not handle the event, the final event is discarded)

The following methods can be used to process the event

Touch start, Finger touch screen-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event//touch end, finger off screen-(void) touchesended: (Nsset<uitouch *> *) touches withevent: (uievent *) event//touch cancellation (e.g. when telephone access)-(void) touchescancelled: ( Nsset<uitouch *> *) touches withevent: (uievent *) event//finger move (will be called multiple times)-(void) touchesmoved: (Nsset<uitouch *> *) touches withevent: (uievent *) 3D Touch event added after Event//3d Touch 9.1-(void) touchesestimatedpropertiesupdated: (Nsset *) Touches

It is important to note thatthere are several situations in which the event cannot be handled by UIView

1.userinteractionenabled=no (Uiimageview's userinteractionenabled default is NO)

2.hidden=yes

3.alpha less than 0.01

3. The development process will also encounter such requirements:

For example, touch a UIView instance object, to jump the page, generally this function is done by the view (Uiviewcontroller) controller,

This way we need to find the view controller for the current view to handle the event, as follows

-(Uiviewcontroller *) viewcontroller{    uiresponder *responder = self.nextresponder;    Do {        if ([Responder Iskindofclass:[uiviewcontroller class]])        {            return (Uiviewcontroller *) responder;        }        responder = Responder.nextresponder;            } while (responder! = nil);        return nil;}

Other objects are required to handle the event in a similar way (as long as the object is on the responder chain)

Event Distribution and responder chain

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.