[Study Notes] responder chain, learning notes chain

Source: Internet
Author: User

[Study Notes] responder chain, learning notes chain

I have been studying iOS for one or two months. I have never prepared my study notes. I plan to make up for it today. I just want to review it and make up for it.

 

There are roughly three types of iOS events: Touch events, accelerator events, and remote control events.

First, we need to understand the following concepts:

1. Target audience:

In iOS, not all objects can process events. Only objects that inherit UIResponder can receive and process events. We call it the "response object ".

UIApplication, UIViewController, and UIView all inherit from UIResponder. Therefore, they are all responder objects that can receive and process events.

Ii. Main methods for handling events by UIResponder

Touch event

- (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;

Accelerator event

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;

Remote Control event

- (void)remoteControlReceivedWithEvent:(UIEvent *)event;

Iii. UITouch

Each finger that you touch at the same time represents a UITouch object, which stores information related to the finger, such as the position, time, and phase of the touch.

When the finger moves, the system updates the same UITouch object so that it can keep the touch position of the finger.

When the finger leaves the (end) screen, the system will destroy the corresponding UITouch object.

 

Iv. UIEvent

When a touch event occurs, a UIEvent object must be generated, called an event object, which records the time and type of the event.

 

5. First, the event Responder

After a touch event occurs, the system adds the event to an event queue managed by UIApplication.

UIApplication extracts the first event from the event queue and distributes the event for processing. Generally, it sends the event to the application's main window (keyWindow) first)

The main window will find the most appropriate view in the view hierarchy to process touch events, which is also the first step in the entire event processing process.

After a proper view control is found, the touches method of the View control is called for specific event processing.

TouchesBegan...

TouchesMoved...

TouchedEnded...

    

The following is an example of the instructor's PPT.

  

Three cases where UIView does not receive touch events

1. alpha = 0.0 ~ 0.01

2. hidden property = YES

3. userInteractionEnabled = YES

Note: userInteractionEnabled of UIImageView is NO by default. Therefore, UIImageView and its sub-controls cannot receive touch events by default.

 

6. response chain

After a suitable control calls the touches method, the event is passed up along the responder chain by default, and the event is handed over to the previous responder for processing.

The process of finding the last responder seems to be the inverse process of finding the most appropriate control.

If [super touches] is called in the touches method of the current responder, the previous responder touches method will be used, and the UITouch object and UIEvent object can be passed up.

In this way, the event object and the information of the touch object of a touch event can be transmitted to multiple contacts.

 

Q: Who was the last Responder (nextResponder?

Judgment steps:
1> if the current view is the Controller view, the controller is the last responder.
2> if the current view is not the Controller view, the parent control is the last responder.
3> if the current responder is a controller, the previous responder is a UIWindow. If the UIWindow is not processed, it is passed to the UIApplication.
4> If the UIApplication does not process the message, the message will be discarded.

  

7. Touch event handling process

1. after the user is touched, the system first transmits the event object from top to bottom (parent control to child control ), find the most suitable control to process events (recursively look for the most suitable child control of the current control)

2. Call touches-related methods for appropriate controls

3. If the super touches method is called, the event will be passed up along the responder chain and passed to the last responder.

4. The touches method of the last responder will be called.

5. As long as the touches method of super is called in the touches method of the current responder, it will continue to be recursively called until super's touches method is no longer called.

 

8. interview questions: What is the response chain?

The following are the answers for your reference:

1> the responder chain is a chain connected by multiple responder objects.

2> the responder object refers to the object that can process the event, that is, the object that inherits the UIResponder.

3> using the responder chain, multiple controls can process the same touch event

4> briefly describe the upstream transmission of the responder chain, and find the previous Responder (nextResponder)

 

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.