iOS 小技能: Responder Chain(响应者链)【上篇】

3,200 阅读5分钟

「这是我参与2022首次更文挑战的第30天,活动详情查看:2022首次更文挑战」。

前言

IOS 中的事件可分为:

  1. 触摸事件(multitouch events)
  2. 加速计事件( accelerometer events)
  3. 远程控制事件(remote control events) | Event type | First responder | | :-------------------- | :---------------------------------------- | | Touch events | The view in which the touch occurred. | | Press events | The object that has focus. | | Shake-motion events | The object that you (or UIKit) designate. | | Remote-control events | The object that you (or UIKit) designate. | | Editing menu messages | The object that you (or UIKit) designate. |

Responder Chain: image.png

I 响应者对象

在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件,我们称之为“响应者对象”。

1.1 触摸事件

UIApplication、UIViewController、UIView都继承于UIResponder。

UIResponder内部提供了以下方法来处理事件

//一根或者多根手指开始触摸view,系统会自动调用view的下面方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
//一根或者多根手指在view上移动,系统会自动调用view的下面方法(随着手指的移动,会持续调用该方法)
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
//一根或者多根手指离开view,系统会自动调用view的下面方法
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
//触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程,系统会自动调用view的下面方法[可选]
- (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;

1.2 加速计事件

- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
 
- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
 
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);

1.3 远程控制事件

- (void)remoteControlReceivedWithEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(4_0);

II UItouch

当用户用一根手指触摸屏幕时,会创建一个与手指相关联的UITouch对象;一根手指对应一个UItouch对象。

2.1 UITouch的作用

保存着跟手指相关的信息,比如触摸的位置、时间、阶段。

1, 当手指移动时,系统会更新同一个UITouch对象,使之能够一直保存该手指的触摸位置。 2. 当手指离开屏幕时,系统会销毁相应的UITouch对象

提示:iPhone开发中,要避免使用双击事件!

2.2 UITouch的属性

////记录了触摸事件产生或变化时的时间,单位是秒The relative time at which the acceleration event occurred. (read-only)
@property(nonatomic,readonly) NSTimeInterval      timestamp;
////当前触摸事件所处的状态
@property(nonatomic,readonly) UITouchPhase        phase;
//// touch down within a certain point within a certain amount of timen短时间内点按屏幕的次数,可以根据tapCount判断单击、双击或更多的点击
@property(nonatomic,readonly) NSUInteger          tapCount;   
@property(nonatomic,readonly) UITouchType         type NS_AVAILABLE_IOS(9_0);
//触摸产生时所处的窗口
@property(nullable,nonatomic,readonly,strong) UIWindow                        *window;
//触摸产生时所处的视图
@property(nullable,nonatomic,readonly,strong) UIView                          *view;
//The gesture-recognizer objects currently attached to the view.
@property(nullable,nonatomic,readonly,copy)   NSArray <UIGestureRecognizer *> *gestureRecognizers

2.3 UITouch的方法

/*返回值表示触摸在view上的位置
这里返回的位置是针对view的坐标系的(以view的左上角为原点(0, 0))
调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置*/
- (CGPoint)locationInView:(nullable UIView *)view;
//该方法记录了前一个触摸点的位置
 
- (CGPoint)previousLocationInView:(nullable UIView *)view;

III UIEvent

每产生一个事件,就会产生一个UIEvent对象,UIEvent称为事件对象,记录事件产生的时刻和类型

3.1 常见属性

  1. 事件类型
@property(nonatomic,readonly) UIEventType     type NS_AVAILABLE_IOS(3_0);
 
@property(nonatomic,readonly) UIEventSubtype  subtype NS_AVAILABLE_IOS(3_0);

2, 事件产生的时间

@property(nonatomic,readonly) NSTimeInterval  timestamp;

3.2 获取touch对象的方法

- (nullable NSSet <UITouch *> *)allTouches;
 
- (nullable NSSet <UITouch *> *)touchesForWindow:(UIWindow *)window;
 
- (nullable NSSet <UITouch *> *)touchesForView:(UIView *)view;
 
- (nullable NSSet <UITouch *> *)touchesForGestureRecognizer:(UIGestureRecognizer *)gesture NS_AVAILABLE_IOS(3_2);
 
 
 
 
// An array of auxiliary UITouch’s for the touch events that did not get delivered for a given main touch. This also includes an auxiliary version of the main touch itself.
 
- (nullable NSArray <UITouch *> *)coalescedTouchesForTouch:(UITouch *)touch NS_AVAILABLE_IOS(9_0);
 
 
 
 
// An array of auxiliary UITouch’s for touch events that are predicted to occur for a given main touch. These predictions may not exactly match the real behavior of the touch as it moves, so they should be interpreted as an estimate.
 
- (nullable NSArray <UITouch *> *)predictedTouchesForTouch:(UITouch *)touch NS_AVAILABLE_IOS(9_0);

IV 什么是响应者链?

iOS中的响应者链(Responder Chain)是用于确定事件响应者的一种机制,其中的事件主要指触摸事件(Touch Event),该机制和UIKit中的UIResponder类紧密相关。 响应触摸事件的都是屏幕上的界面元素,而且必须是继承自UIResponder类的界面类(包括各种常见的视图类及其视图控制器类,如UIView和UIViewController)才可以响应触摸事件。

  • 一个事件响应者的完成主要经过两个过程:hitTest方法命中视图和响应者链确定响应者。 hitTest方法首先从顶部UIApplication往下调用(从父类到子类),直到找到命中者,然后从命中者视图沿着响应者链往上传递寻找真正的响应者。

4.1.命中测试

命中测试(hitTest)主要会用到视图类的hitTest函数和pointInside函数。

其中,前者用于递归寻找命中者,后者则是检测当前视图是否被命中,即触摸点坐标是否在视图内部。

当触摸事件发生后,系统会将触摸事件以UIEvent的方式加入到UIApplication的事件队列中,UIApplication将事件分发给根部的UIWindow去处理,UIWindow则开始调用hitTest方法进行迭代命中检测。

  • 命中检测具体迭代的过程为:如果触摸点在当前视图内,那么递归对当前视图内部所有的子视图进行命中检测;如果不在当前视图内,那么返回NO停止迭代。这样最终会确定屏幕上最顶部的命中的视图元素,即命中者。

4.2.响应者链

通过命中测试找到命中者后,任务并没有完成,因为最终的命中者不一定是事件的响应者。 所谓的响应就是开发中为事件绑定的一个触发函数,事件发生后执行响应函数里的代码,例如通过addTarget方法为按钮的单击事件绑定响应函数,在按钮被单击后能及时执行想要执行的任务。

see also

iOS 小技能:响应者链的事件传递过程:https://kunnan.blog.csdn.net/article/details/74107917

🍅 联系作者: iOS逆向(公号:iosrev)


🍅 作者简介:CSDN 博客专家认证🏆丨全站 Top 50、华为云云享专家认证🏆、iOS逆向公号号主


🍅 简历模板、技术互助。关注我,都给你。