当前位置:网站首页>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!
边栏推荐
猜你喜欢
随机推荐
不依赖框架的文件下载
单片机第一步
【动态规划】LeetCode刷题清单及思路记录
【Exhibition of some projects】
如何将modelsim仿真数据存成文件
D-Desthiobiotin Amine_D-脱硫生物素-胺相关的产品性质
Unity 渲染流水线
如何使用xilinx的FFT ip
牛顿迭代法求方程的根
Biotin-C6-amine_N-生物素基-1,6-己二胺_CAS:65953-56-2_100mg
如何将matlab数据导入modelsim仿真
04-加壳和脱壳
Waterfall flow (custom layout implementation)
GadgetInspector原理分析
This beta version of Typora is expired, please download and install a newer; workaround
xftp的简单使用
测试第二题
读取GZ包的内容代码
图扑软件携手华为云再创合作共赢新局面
Unity Shader的结构与语义