The concept of a responder

Source: Internet
Author: User

Responder objects (Response object)

The responder object is the ability to respond to events and handle events. In iOS, there is a Uiresponder class that defines all the methods of the responder object. UIApplication, UIView and other classes inherit the Uiresponder class, and the controls in UIWindow and Uikit inherit UIView, so they inherit uiresponder classes indirectly, and instances of these classes can be used as responders.

First Responder (responder):

The responder object that is currently receiving touch is called the first responder, which means that the object is currently interacting with the user, which is the beginning of the responder chain.
Responder chain (Responder chain):

The responder chain represents a series of responder objects. The event is referred to the first responder object, and if the first responder is not processed, the event is passed up along the responder chain and handed over to the next responder (next responder). In general, the first responder is a view object or its child class object, when it is touched after the event is processed, if it is not processed, the event will be passed to its view controller object (if present), then its parent view (Superview) object (if present), and so on, until the top-level view. Next, the top view is followed by the window (UIWindow object) to the program (UIApplication object). If the entire process does not respond to this event, the event is discarded. In general, events stop passing in the responder chain as long as the event is handled by the object. However, it is sometimes possible to determine whether or not to continue passing events in the view's response method based on some criteria.


Manage Event Distribution

Whether the view needs to respond to touch events can be done by setting the Userinteractionenabled property of the view. The default state is yes, and if set to No, you can prevent the view from receiving and distributing touch events. In addition, when the view is hidden (sethidden:yes) or transparent (the alpha value is 0), the event is not received. However, this property is only valid on the view, and if you want the entire program to step in response to the event, you can call UIApplication's Beginingnoringinteractionevents method to completely stop receiving and distributing the event. The Endingnoringinteractionevents method is used to restore the program to receive and distribute events. If you want the view to receive multi-touch, you need to set its Multipletouchenabled property to Yes, by default this property value is no, that is, the view does not receive multi-touch by default.

Handling User Touch Events

The first touch of the object is the view, and the view's class UIView inherits the Uirespnder class, but to handle the event, you also need to override the event handler defined in the Uiresponder class. Depending on the touch state, the program invokes the corresponding handler function, which includes the following:

-(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 Touchesbegan:withevent method is called when the finger touches the screen;

When the finger moves on the screen, the Touchesmoved:withevent method is called;

The Touchesended:withevent method is called when the finger leaves the screen;

The Touchescancelled:withevent method is called when the touch is canceled (such as when a call is interrupted during a touch). When these methods are called, they correspond exactly to the 4 enumerated values of the phase property in the Uitouch class.

The above four event methods, which do not require full implementation during development, can override specific methods as needed. For these 4 methods, there are two identical parameters: Nsset type touches and uievent type of event. Where touches represents all Uitouch objects produced by the touch, and event represents a specific event. Because uievent contains all touch objects throughout the touch process, you can call the Alltouches method to get all the touch objects within that event, You can also call Touchesforview: or touchesforwindows: Remove a particular view or touch object on the window. In these events, you can get a touch object and then do the logical processing based on its position, state, and time attributes.

The concept of a responder

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.