当前位置:网站首页>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!
边栏推荐
- -----博客声明
- arthas常用命令
- 实现二叉树--实现删除
- 数码管动态显示及模块化编程
- 网络协议03 - 路由和NAT
- STM32F103连接L9110S电机驱动控制小水泵
- Biotin-PEG4-DADPS-Picolyl-azide(CAS:2599839-59-3)生物素试剂
- This beta version of Typora is expired, please download and install a newer; workaround
- Biotinyl Cystamine_CAS:128915-82-2_生物素半胱胺
- Azide-SS-biotin|CAS:1620523-64-9|生物素-二硫键-叠氮可降解 (cleavable) 的 ADC linke
猜你喜欢
随机推荐
2020-09-03 Solve the very slow installation of pip install [Errno 101] Network unreachable problem
libgrape-lite on GPUs:GPU助力加速图分析任务
数码管动态显示及模块化编程
ES6语法笔记(ES6~ES11)
DADPS-生物素-炔基_CAS:2241685-22-1试剂反应原理
C 语言之学生管理系统-多文件编程
力扣题解7.27
用于标记蛋白质和抗体的Biotin-LC-Sulfo-NHS|CAS:191671-46-2
比尔·盖茨买百万亩农地成美“头号地主”,图扑数字孪生农场
为数字政府构建智能化网络安全管控体系
顺序二叉树---实现数组的二叉树前序遍历输出
【部分项目展示】
C#最优二叉树----哈夫曼树
Delete all files containing a keyword in the current path
Ingress:从静态图分析到动态图分析
【无标题】
瀑布流(自定义布局实现)
js高级学习笔记(详细)
网络协议01 - 基础概念
从安装到编译: 10分钟教你在本地使用和开发GraphScope
![[Common usage of markdown]](/img/4b/3cb17b1dafe095e2f45510b41186fd.png)








