The transmission and response mechanism of touch events in iOS

The transmission and response mechanism of touch events in iOS

All inherited responder objects UIResponder can receive and process events. In chronological order, the most suitable view is found first, and then the view's touches method is called. The default behavior of these methods is to pass the event up the response chain and hand the event over to the previous responder for processing until a view can handle the response or discard it. This is a top-down and then bottom-up process.

1. Event Generation

  • After an event occurs, the system adds the event to an event queue managed by UIApplication.
  • UIApplication will take the first event from the event queue and dispatch it for processing. Usually, the event is sent to the main window (keywindow) of the application first.
  • KeyWindow will find the most appropriate view in the view hierarchy to handle the event.

2. Event delivery

The event is first passed from the parent control to the child control (UIApplication->window->find the most suitable view to handle the event).

If the parent view cannot receive touch events, then the child view cannot receive touch events either.

2.1 How to find the most appropriate view to handle events

Determine whether keywindow accepts events

Determine whether the incident happened to you

Traverse the sub-controls in the sub-view array from back to front, repeating the previous steps 1 and 2

If there is no suitable child control, then you are the most suitable control

2.2 Find the most suitable view bottom-level analysis

There are two important methods:

  1. hitTest:withEvent:
  2. pointInside:withEvent:

The view will call the hitTest:withEvent: method, which will call the pointInside:withEvent: method to determine whether the touch point is in the coordinate system of this view. If it is, the event will be distributed to the child views of this view. Then each child view will repeat the above steps until a suitable view is found at the top level.

3. Response to incidents

The event response will start from the most appropriate view at the bottom layer, and then respond to touch events layer by layer along the chain found in the previous step. By default, the touch event will be passed to the upper layer. If it reaches the view of the viewcontroller, it will be passed to the viewcontroller. If the viewcontroller cannot handle it, it will be passed to UIWindow. If UIWindow cannot handle it, it will be passed to UIApplication. If UIApplication cannot handle it, it will be passed to UIApplicationDelegate. If UIApplicationDelegate cannot handle it, it will discard the event.

<<:  Looking back at the past decade of iPhone suppliers: some made a lot of money, while others lost a lot

>>:  Aiti Tribe Stories (34): Self-cultivation of a full-stack engineer

Recommend

Summary and conclusion of Apple Store and Google Play app releases!

App release has become a crucial part of the prod...

Google Glass Cost Analysis

Google Glass is about to be released, and the pric...

Case! 3 major steps for APP to acquire new users

A store without customers will close, and a produ...

Increase the conversion rate of bidding accounts by 20%?

Whether it is a search account or an information ...

The next trend of smart hardware has not yet really appeared

Nowadays, there are too many entrepreneurial proj...

National trend marketing methodology in 2022!

Although the issue of national trends is always m...

Li Xingyu | Journey to the West

"In Xinjiang, almost every family has a duta...

The 5 most worth-watching Spring Festival marketing cases in 2019!

Whenever the Spring Festival approaches, going ho...

Double 11 is here! How do those bidding veterans operate their accounts?

" Double Eleven has begun, has your traffic ...

One picture to understand | China's radio development

【2.13 World Radio Day】One picture to understand |...