Event handling, responder chain, and first responder in iOS

Source: Internet
Author: User

In iOS, the event Uievent class indicates that when an event occurs, the system collects related event information, creates an Uievent object, and finally forwards the event to the Application object (uiapplication). In daily life, there are three main types of events: Touch events, accelerometer events, and remote control events. Here is an official picture:

When a user triggers an event by doing so, the corresponding event object is added to the UIApplication event queue. The uiapplication loops the first event to be processed from the queue. This event is first distributed to UIApplication's main Window object (Keywindow), and then the main window determines how the event is handed to the most appropriate responder (Uiresponder) to handle depending on the type of event. There are two main types of situations:

1. Touch event: UIApplication A touch detection to determine the most appropriate response to handle the event, in general, the responder is the UIView object.

2. Accelerometer event or remote control event: UIApplication find the first responder in UIWindow. When the first responder is found, the event object is distributed to the responder for processing. Responder

The above two cases are discussed below respectively.

First, touch detection in touch events

First we need to make sure that a UIView object is able to receive touch events with at least the following three conditions:

1. The Userinteractionenabled property is yes, which means that the control is allowed to interact with the user.

2, the Hidden property is No. Controls are invisible, what do they touch?

3, opacity attribute value 0 ~0.01, not transparent too much?

The next we just think that the three basic attributes are satisfied with the requirements, easy to describe, of course, not meet the requirements of the nature can not receive the touch said events.

When the user's finger touches an area of the screen, UIWindow looks for its child controls, and then invokes the method of all self-control parts:

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

To handle the touch event using the specified touch point to get the most appropriate uiview. How to get the UIView principle by touch point is actually very simple, just need to check whether the touch point is within the rectangular area of the control, in fact, the Hittest:withevent method is also called method inside:

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

Returns YES if an incoming control is detected to contain the touch point.

When the UIView is detected by the HitTest method, the UIView object will continue to be detected once, which is to find the subviews of subviews to do touch detection. Eventually, the method returns the most appropriate control to respond to the event. Again, if the previous three conditions are not met, then the UIView and its subviews cannot respond to the touch event.

When a responder is found, the responder can override the following methods to respond to touch events:

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{    [Super Touchesbegan:touches withevent:event];//allows the next responder to have the opportunity to continue processing}-(void) touchesmoved: (Nsset<uitouch *> *) touches withevent: (Uievent *) event{    [Super Touchesbegan:touches withevent:event];} -(void) touchesended: (Nsset<uitouch *> *) touches withevent: (uievent *) event{    [Super Touchesbegan:touches Withevent:event];} -(void) touchescancelled: (Nsset<uitouch *> *) touches withevent: (uievent *) event{    [Super Touchesbegan: Touches withevent:event];}

Inside the response method, we can also continue to pass this touch event to the parent control's corresponding method. The parent control can then continue to pass the event up until it is passed to the UIApplication object. This series of responder objects constitutes a chain of responders.

Second, first responder (Responder)

What is the first responder? In a nutshell, the first responder is an object that the UIWindow object receives after an event, the first one to respond to the event. Note: This first responder is not a concept with the first response detected by the touch previously discussed in UIView. The first responder is typically used to handle non-touch events (mobile phone shaking, remote space for headphone line control) or touch events (keyboard touch events) other than this window, and the popular point is actually the responder who cares about other people's business. In iOS, of course, not all controls are willing to, so it seems not very good to understand, or is to stand in the programmer's view of the problem, the programmer is responsible for telling the system which object can become the first responder (Canbecomefirstresponder), If the method Canbecomefirstresponder returns Yes, the responder object is eligible to be called the first responder. Eligibility does not mean that certain can become the first responder, as if the requirements do not necessarily be able to apply for success, so also a hiring link, that is Becomefirstresponder formally become the first responder.

Please forgive me these may not be normal ideas, personally feel the process is a bit like the recruitment process, resume screening is Canbecomefirstresponder,becomefirstresponder is formally become the company's employees. So since the company by hiring, then the corresponding to the dismissal! The corresponding method is Canresignfirstresponder, this indicates whether the first responder can be dismissed, some good to the inverse of the staff is not to say that the dismissal of the dismissal, to fight one day can become this counter-day staff, OK, I pulled away again. Another way is to Resignfirstresponder and formally dismiss the employee.

It is worth noting that a UIWindow object can only have one responder object at a time to become the first responder. We can determine whether an object is the first responder by Isfirstresponder.

Let's look at one of the following mobile interfaces:

  

The interface contains two input boxes, a button that toggles the first responder. I bound the start Edit event for two input boxes and then printed the first responder-related information in the event code as follows:

NSString * Nsstringfromboolvalue (BOOL boolvalue) {    return boolvalue? @ "YES": @ "NO";} -(Ibaction) Editingbegin: (ID) Sender {    NSLog (@ "Top: can be the first responder =>%@, whether the first responder =>%@", Nsstringfromboolvalue ( Self.topInputView.canBecomeFirstResponder), Nsstringfromboolvalue (Self.topInputView.isFirstResponder));    NSLog (@ "down: whether it can be the first responder =>%@, whether the first responder =>%@", Nsstringfromboolvalue ( Self.downInputView.canBecomeFirstResponder), Nsstringfromboolvalue (Self.downInputView.isFirstResponder));}

When you click the first input box, print as follows:

  

We can see that two input boxes can be the first responders. But only the first input box is the first responder.

When you click the second input box, print as follows:

  

We can see that two input boxes are the first responders, but only the following input box is the first responder.

We notice that there is a button below the two input boxes to toggle the first responder. The response event method for the button is:

  

-(ibaction) switch: (ID) Sender {    /**     *  1, if the top input box is the first responder, switch the first responder to the input box below        2, if the top input box is not the first responder, It is set to the first responder.     *    /if (self.topInputView.isFirstResponder) {        [Self.downinputview becomefirstresponder];    } else{        [Self.topinputview becomefirstresponder];    }        NSLog (@ "The first responder in the parent control:%@", [Self.uiview Findfirstresponder]);        NSLog (@ "Top: Whether it can be the first responder =>%@, whether the first responder =>%@", Nsstringfromboolvalue (Self.topInputView.canBecomeFirstResponder ),
Nsstringfromboolvalue (Self.topInputView.isFirstResponder)); NSLog (@ "down: whether it can be the first responder =>%@, whether the first responder =>%@", Nsstringfromboolvalue ( Self.downInputView.canBecomeFirstResponder),
Nsstringfromboolvalue (Self.downInputView.isFirstResponder)); }

After clicking on the first input box, we click Switch First Responder, the screen prints as follows:

As we can see, the input box below this time becomes the first responder, and the start edit event is triggered, so there are two printing times. After switching, the focus also switches to the second input box, and when we enter through the keyboard, the content appears in the second input box.

Often someone encounters a problem that wants to click on an empty area to hide the keyboard. For example, input box input half, feel no longer want to edit, you can click on the blank area to hide the keyboard, this time actually only need to tell the system, this first responder position I do not want, I want to resign, this is enough! Here is the click of the first input box, and then click on the blank area, triggering the Touchesbegin event, the last Topinputview resignation process, in this way you can hide the keyboard. The code is as follows:

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{    [Self.topinputview Resignfirstresponder];}

  

Write it here today and follow up with some of the finer things.

  

 

Event handling, responder chain, and first responder in iOS

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.