当前位置:网站首页>Event Delivery and Responder Chains
Event Delivery and Responder Chains
2022-07-30 07:36:00 【rhubarb_yellow】
event in ios
First of all, there are three types of events in iOS:
- Touch events
- Accelerometer Events
- Remote control events
Then the main explanation of this article is touch events!
event generation and delivery
The delivery process is as follows:
After a touch event occurs, the system will add the time to an event queue managed by UIApplication
UIApplication will take the front event from the event queue, and then dispatch the event for processing. Usually, the event is sent to the application's main window (keywindow) first
The main window will find a view in the view hierarchy that best handles the event to handle
After finding a suitable view control, it will call the touch method of the view control to do specific event processing
- touchesBegan…
- touchesMoved…
- touchesEnded..
example:

In the above picture, if we click on the green View, then the event delivery is:
UIApplication -> UIWindow -> White View -> Green View
So this leads to a conclusion: If the parent control can't accept touch events, then the child control can't accept touch events
How to find the most suitable controls to handle events?
This solution follows a set of principles:
- Can I receive touch events myself?
- Is the touch point on yourself?
- Traverse the child controls from back to front, repeat the above two steps (recursive)
- If there is no suitable child control, then it is best to handle it by yourself
This principle is the underlying implementation principle of the hitTest method. At the same time, the hitTest also calls the pointInside method to determine whether the touch point is on itself.
Example:
As shown in the figure above, if the order of adding views is: white View first add green View, then orange View, orange View first add blue View, then red View, and yellow View in blue View
Detailed process of event processing: At this time, we click on the orange View, then first UIWindow will accept the event (the above two are satisfied), then find the child control white View (also satisfied), and then find the sub-controlThe orange View of the control (the View is added later and is also satisfied), so find the red View of the sub-control (added later, the touch point is not on yourself, not satisfied), and then find the blue View of the sub-control (the touch point is not on yourself,Not satisfied), since none of the orange View's child controls meet the above conditions, so handle the event by yourself
Three cases where the view does not accept touch events
userInteractionEnabled = NO, no user interaction is accepted
hidden = YES, hidden
alpha = 0.0~0.01, transparent
Tips: UIImageView default userInteractionEnabled is NO, so UIImageView and its child controls cannot accept touch events by default
Detailed process of touch event handling
A touch event is generated after the user taps the screen. After a series of transmissions, the most appropriate view control will be found to handle the touch event
After finding the most suitable view control to handle the touch event, it will call the touches method of the view control to do specific event handling
The default method of these touches methods is to pass the event up according to the responder chain, and hand over the event to the previous responder object for processing (that is, to judge whether the current responder implements the touches method, if it is not implemented, the event will be passed by defaultto the previous responder)
Then another concept is derived: What is a responder and what is a responder chain?
responder object, responder chain
In iOS, not any object can handle events, only objects inherited from UIResponder can accept and handle events, we call it "responder object"
The responder chain is shown below: 
A chain of many responders linked together is called a responder chain!
Then how to judge who is the last responder object of the current responder?
Determine whether the current view is the controller's view, if it is the controller's view, then the last responder is the controller
If the current view is not the controller's view, the last responder is the parent control
The top-level view in the view hierarchy, if it can't handle the received event or message, it will pass the event or message to the window object for processing, and if the window object can't handle it, it will pass the event or message.Give the UIApplication object, if the UIApplication object can't handle the event or message either, throw it away!
What does the responder chain do?
You can make multiple responders respond at the same time when a touch event occurs!
边栏推荐
- 2021-05-26
- Azide-SS-biotin|CAS:1620523-64-9|生物素-二硫键-叠氮可降解 (cleavable) 的 ADC linke
- vscode set sublime theme
- 基于精灵(Sprite)管道烟雾流动效果
- C语言,库函数中qsort的用法,及解释
- 【部分项目展示】
- 用 GraphScope 像 NetworkX 一样做图分析
- JSP自定义标签
- xxx is not in the sudoers file.This incident will be reported error
- This beta version of Typora is expired, please download and install a newer;解决方法
猜你喜欢
随机推荐
NS3 error fatal error: ns3/opengym-module.h: No such file or directory
测试第二题
------实现二叉搜索树BST
掌握JESD204B(一)–AD6676的调试
SQL并列排序问题
Unity Shader标准光照模型——高光反射
The IEEE under the specified journal search related papers
C#最优二叉树----哈夫曼树
Unable to open socket file: target process not responding or HotSpot VM not loaded
The most complete difference between sizeof and strlen, as well as pointer and array operation analysis
图扑软件携手华为云再创合作共赢新局面
Insert map data efficiently
基于精灵(Sprite)管道烟雾流动效果
图扑数字孪生北京故宫,推进旅游业元宇宙进程
Biotin-C6-amine_N-生物素基-1,6-己二胺_CAS:65953-56-2_100mg
Deep Interpretation of void (*signal(int , void(*)(int)))(int) in "C Traps and Defects"
力扣题解7.27
rsync使用方法之坑
基于 JupyterLab 插件在 GraphScope 中交互式构图
com.alibaba.datax.common.exception.DataXException: Code:[ESWriter-03]









