一、响应链
响应链主要是对UIResponder及其子类事件传递的描述。
UIViewController及UIView都是继承UIResponder的。
而其中的UIView更是UIKIT框架下大部分控件的父类(几乎所有吧),所以能够响应事件的对象,实质上都相当于不断拦截转发至最后的可响应对象。
=========================================================
如下图,可以看到UIResponder的整个结构:
================================================
可以看到我们熟悉的touch事件,
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullableUIEvent *)event;
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullableUIEvent *)event;
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullableUIEvent *)event;
- (void)touchesCancelled:(nullableNSSet<UITouch *> *)touches withEvent:(nullableUIEvent *)event;
- (void)touchesEstimatedPropertiesUpdated:(NSSet *_Nonnull)touchesNS_AVAILABLE_IOS(9_1);
以及常用的对Responder的处理
- (nullableUIResponder*)nextResponder;
- (BOOL)canBecomeFirstResponder; // default is NO
- (BOOL)becomeFirstResponder;
- (BOOL)canResignFirstResponder; // default is YES
- (BOOL)resignFirstResponder;
- (BOOL)isFirstResponder;
所以对于继承UIResponder的所有对象,都可以对touch事件进行重写,拦截事件处理,而如果不进行处理,则最后会交给基类的UIResponder进行丢弃。
二、对于touch事件传递
1.获取事件发生的view
在UIView中有如下方法:
- (nullableUIView *)hitTest:(CGPoint)point withEvent:(nullableUIEvent *)event
- (BOOL)pointInside:(CGPoint)point withEvent:(nullableUIEvent *)event;
事件发生的view就是hitTest返回的view,而得到这个view则需要pointInside通过point来确定view是否在点击point的范围内,不断的递归得到最上层的view。
2.touch事件传递
对于touch事件,windows对象首先会将事件传递给事件发生的view(上面提到的view),
如果这个view不处理事件,则事件响应链将事件传给下一个Responder(我们可以通过nextResponder得知下一个响应者,基本上是不断的superview->ViewController->UIWindows->UIApplication->AppDelegate(AppDelegate继承于UIResponder)),
直至有对象响应,或者到AppDelegate(UIResponder)则遗弃该事件。