iOS 响应链探索-UIResponder
首先要先了解响应者对象UIResponder,只有继承UIResponder的的类,才能处理事件。
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIApplication : UIResponder
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusItem, CALayerDelegate>
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIViewController : UIResponder <NSCoding, UIAppearanceContainer, UITraitEnvironment, UIContentContainer, UIFocusEnvironment>
我们可以看出UIApplication,UIView,UIViewController都是继承自UIResponder类,可以响应和处理事件。
我们下边就来探索一下 响应链
首先 我们分别在自定义的UIWindow,UIView,UIViewController的View
中复写下边几个方法,添加打印 ,看一下打印顺序
// 判断一个点是否落在范围内
- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event{
BOOL isInside = [super pointInside:point withEvent:event];
SGTLog(@" -------- pointInside point %@ isInside %d Event %@",NSStringFromCGPoint(point),isInside,event);
return isInside;
}
// 此方法返回的View是本次点击事件需要的最佳View
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event{
SGTLog(@" -------- hitTest point %@ Event %@",NSStringFromCGPoint(point),event);
return [super hitTest:point withEvent:event];
}
//开始触摸
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
SGTLog(@"----- touchesBegan touches %@ event %@",touches,event);
}
//滑动
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
SGTLog(@" ---- touchesMoved touches %@ event %@",touches,event);
}
//触摸结束
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
SGTLog(@" ---- touchesEnded touches %@ event %@",touches,event);
}
//触摸取消
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
SGTLog(@" ---- touchesCancelled touches %@ event %@",touches,event);
}
- (void)touchesEstimatedPropertiesUpdated:(NSSet<UITouch *> *)touches{
SGTLog(@" ---- touchesEstimatedPropertiesUpdated touches %@ ",touches);
}
点击屏幕输出日志如下:
2019-04-27 10:32:29.795751+0800 ResponderDemo[67074:1346842]
-[SGTWindow hitTest:withEvent:] [SGTWindow.m Line:34]
-------- fitView <RedView: 0x7fcf04e0afc0; frame = (100 100; 200 100); layer = <CALayer: 0x6000024b1220>>
2019-04-27 10:32:29.796317+0800 ResponderDemo[67074:1346842]
-[SGTWindow hitTest:withEvent:] [SGTWindow.m Line:21]
-------- hitTest point {173, 176.66667175292969} Event <UITouchesEvent: 0x6000016f47e0> timestamp: 201623 touches: {(
)}
2019-04-27 10:32:29.809877+0800 ResponderDemo[67074:1346842]
-[SGTWindow pointInside:withEvent:] [SGTWindow.m Line:16]
-------- pointInside point {173, 176.66667175292969} isInside 1 Event <UITouchesEvent: 0x6000016f47e0> timestamp: 201623 touches: {(
)}
2019-04-27 10:32:29.810082+0800 ResponderDemo[67074:1346842]
-[SGTBaseView hitTest:withEvent:] [SGTBaseView.m Line:22]
-------- hitTest point {173, 176.66667175292969} Event <UITouchesEvent: 0x6000016f47e0> timestamp: 201623 touches: {(
)}
2019-04-27 10:32:29.810289+0800 ResponderDemo[67074:1346842]
-[SGTBaseView pointInside:withEvent:] [SGTBaseView.m Line:17]
-------- pointInside point {173, 176.66667175292969} isInside 1 Event <UITouchesEvent: 0x6000016f47e0> timestamp: 201623 touches: {(
)}
2019-04-27 10:32:29.810448+0800 ResponderDemo[67074:1346842]
-[OrangeView hitTest:withEvent:] [OrangeView.m Line:24]
-------- hitTest point {-97, 26.666671752929688} Event <UITouchesEvent: 0x6000016f47e0> timestamp: 201623 touches: {(
)}
2019-04-27 10:32:29.810640+0800 ResponderDemo[67074:1346842]
-[OrangeView pointInside:withEvent:] [OrangeView.m Line:19]
-------- pointInside point {-97, 26.666671752929688} isInside 0 Event <UITouchesEvent: 0x6000016f47e0> timestamp: 201623 touches: {(
)}
2019-04-27 10:32:29.810813+0800 ResponderDemo[67074:1346842]
-[RedView hitTest:withEvent:] [RedView.m Line:25]
-------- hitTest point {73, 76.666671752929688} Event <UITouchesEvent: 0x6000016f47e0> timestamp: 201623 touches: {(
)}
2019-04-27 10:32:29.811021+0800 ResponderDemo[67074:1346842]
-[RedView pointInside:withEvent:] [RedView.m Line:20]
-------- pointInside point {73, 76.666671752929688} isInside 1 Event <UITouchesEvent: 0x6000016f47e0> timestamp: 201623 touches: {(
)}
2019-04-27 10:32:29.811395+0800 ResponderDemo[67074:1346842]
-[SGTBaseView hitTest:withEvent:] [SGTBaseView.m Line:35]
-------- fitView <RedView: 0x7fcf04e0afc0; frame = (100 100; 200 100); layer = <CALayer: 0x6000024b1220>>
2019-04-27 10:32:29.811686+0800 ResponderDemo[67074:1346842]
-[SGTWindow hitTest:withEvent:] [SGTWindow.m Line:34]
-------- fitView <RedView: 0x7fcf04e0afc0; frame = (100 100; 200 100); layer = <CALayer: 0x6000024b1220>>
2019-04-27 10:32:29.814371+0800 ResponderDemo[67074:1346842]
-[RedView touchesBegan:withEvent:] [RedView.m Line:48]
----- touchesBegan touches {(
<UITouch: 0x7fcf04d03df0> phase: Began tap count: 1 force: 0.000 window: <SGTWindow: 0x7fcf04d032f0; baseClass = UIWindow; frame = (0 0; 375 812); gestureRecognizers = <NSArray: 0x600002abf510>; layer = <UIWindowLayer: 0x6000024f3c40>> view: <RedView: 0x7fcf04e0afc0; frame = (100 100; 200 100); layer = <CALayer: 0x6000024b1220>> location in window: {173, 176.66665649414062} previous location in window: {173, 176.66665649414062} location in view: {73, 76.666656494140625} previous location in view: {73, 76.666656494140625}
)} event <UITouchesEvent: 0x6000016f47e0> timestamp: 201623 touches: {(
<UITouch: 0x7fcf04d03df0> phase: Began tap count: 1 force: 0.000 window: <SGTWindow: 0x7fcf04d032f0; baseClass = UIWindow; frame = (0 0; 375 812); gestureRecognizers = <NSArray: 0x600002abf510>; layer = <UIWindowLayer: 0x6000024f3c40>> view: <RedView: 0x7fcf04e0afc0; frame = (100 100; 200 100); layer = <CALayer: 0x6000024b1220>> location in window: {173, 176.66665649414062} previous location in window: {173, 176.66665649414062} location in view: {73, 76.666656494140625} previous location in view: {73, 76.666656494140625}
)}
2019-04-27 10:32:29.920262+0800 ResponderDemo[67074:1346842]
-[RedView touchesEnded:withEvent:] [RedView.m Line:56]
---- touchesEnded touches {(
<UITouch: 0x7fcf04d03df0> phase: Ended tap count: 1 force: 0.000 window: <SGTWindow: 0x7fcf04d032f0; baseClass = UIWindow; frame = (0 0; 375 812); gestureRecognizers = <NSArray: 0x600002abf510>; layer = <UIWindowLayer: 0x6000024f3c40>> view: <RedView: 0x7fcf04e0afc0; frame = (100 100; 200 100); layer = <CALayer: 0x6000024b1220>> location in window: {173, 176.66665649414062} previous location in window: {173, 176.66665649414062} location in view: {73, 76.666656494140625} previous location in view: {73, 76.666656494140625}
)} event <UITouchesEvent: 0x6000016f47e0> timestamp: 201623 touches: {(
<UITouch: 0x7fcf04d03df0> phase: Ended tap count: 1 force: 0.000 window: <SGTWindow: 0x7fcf04d032f0; baseClass = UIWindow; frame = (0 0; 375 812); gestureRecognizers = <NSArray: 0x600002abf510>; layer = <UIWindowLayer: 0x6000024f3c40>> view: <RedView: 0x7fcf04e0afc0; frame = (100 100; 200 100); layer = <CALayer: 0x6000024b1220>> location in window: {173, 176.66665649414062} previous location in window: {173, 176.66665649414062} location in view: {73, 76.666656494140625} previous location in view: {73, 76.666656494140625}
)}
我们看到 主要应用到下边两个方法
// 此方法返回的View是本次点击事件需要的最佳View
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
// 判断一个点是否落在范围内
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
这个两个方法的作用是寻找到最合适的View去响应事件,我们先了解下事件的分发和传递,更便于我们了解流程:
1.当iOS程序中发生触摸事件后,系统会将事件加入到UIApplication管理的一个任务队列中
2.UIApplication将处于任务队列最前端的事件向下分发。即UIWindow。
3.UIWindow将事件向下分发,即UIView。
4.UIView首先看自己是否能处理事件,触摸点是否在自己身上。如果能,那么继续寻找子视图。
5.遍历子控件,重复以上两步。
6.如果没有找到,那么自己就是事件处理者。如果
7.如果自己不能处理,那么不做任何处理。
其中 UIView不接受事件处理的情况主要有以下三种
1)alpha <0.01
2)userInteractionEnabled = NO
3.hidden = YES.
结合我们的日志输出,我们可以看到,首先调起的是Widow的hitTest:withEvent:方法,满足(alpha >=0.01
&& userInteractionEnabled == YES && hidden == NO.) 调用pointInside:withEvent:方法判断是否在当前响应区域,如果在当前区域,从后往前轮询自己的子视图,调用子视图的hitTest:withEvent:方法,一直找到最合适的响应View,返回,代码如下:
// 此方法返回的View是本次点击事件需要的最佳View
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event{
SGTLog(@" -------- hitTest point %@ Event %@",NSStringFromCGPoint(point),event);
// 1.判断当前控件能否接收事件
if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) return nil;
// 2. 判断点在不在当前控件
if ([self pointInside:point withEvent:event] == NO) return nil;
// 3.从后往前遍历自己的子控件
NSInteger count = self.subviews.count;
for (NSInteger i = count - 1; i >= 0; i--) {
UIView *childView = self.subviews[i];
// 把当前控件上的坐标系转换成子控件上的坐标系
CGPoint childP = [self convertPoint:point toView:childView];
UIView *fitView = [childView hitTest:childP withEvent:event];
if (fitView) { // 寻找到最合适的view
SGTLog(@" -------- fitView %@",fitView);
return fitView;
}
}
// 循环结束,表示没有比自己更合适的view
return self;
// return [super hitTest:point withEvent:event];
}
响应者链及nextResponder
响应者链
响应链是从最合适的view开始传递,处理事件传递给下一个响应者,响应者链的传递方法是事件传递的反方法,如果所有响应者都不处理事件,则事件被丢弃。我们通常用响应者链来获取上几级响应者,方法是UIResponder的nextResponder方法。
我们从上边日志中找到最合适的View是RedView,所以RedView是响应者,最终实现的是RedView的touchesBegan:withEvent:及touchesEnded:withEvent:方法,我们可以在RedView的touchesBegan或者其他方法中处理逻辑,另外如果我们想把事件传递到RedView的父视图,可以使用nextResponder来获取,使用[self.nextResponder touchesBegan:touches withEvent:event];方法可以把响应传递到下一个响应者(一般为父视图)
//开始触摸
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
SGTLog(@" ----- touchesBegan touches %@ event %@",touches,event);
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}
nextResponder
我们有时候可能会通过nextResponder来查找控件的父视图控件。
// 通过遍历view上的响应链来查找SuperView
UIResponder *responder = self.nextResponder;
while (responder) {
if ([responder isKindOfClass:[SGTBaseView class]]) {
SGTBaseView *superView = (SGTBaseView *)responder;
SGTLog(@" -------- superView %@ ",superView);
break;
}
responder = responder.nextResponder;
}