首先要先了解響應(yīng)者對(duì)象UIResponder肠缨,只有繼承UIResponder的的類(lèi),才能處理事件盏阶。
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類(lèi)名斟,可以響應(yīng)和處理事件脑慧。
我們下邊就來(lái)探索一下 響應(yīng)鏈
首先 我們分別在自定義的UIWindow,UIView砰盐,UIViewController的View
中復(fù)寫(xiě)下邊幾個(gè)方法闷袒,添加打印 ,看一下打印順序
// 判斷一個(gè)點(diǎn)是否落在范圍內(nèi)
- (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是本次點(diǎn)擊事件需要的最佳View
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event{
SGTLog(@" -------- hitTest point %@ Event %@",NSStringFromCGPoint(point),event);
return [super hitTest:point withEvent:event];
}
//開(kāi)始觸摸
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
SGTLog(@"----- touchesBegan touches %@ event %@",touches,event);
}
//滑動(dòng)
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
SGTLog(@" ---- touchesMoved touches %@ event %@",touches,event);
}
//觸摸結(jié)束
- (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);
}
點(diǎn)擊屏幕輸出日志如下:
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}
)}
我們看到 主要應(yīng)用到下邊兩個(gè)方法
// 此方法返回的View是本次點(diǎn)擊事件需要的最佳View
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
// 判斷一個(gè)點(diǎn)是否落在范圍內(nèi)
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
這個(gè)兩個(gè)方法的作用是尋找到最合適的View去響應(yīng)事件岩梳,我們先了解下事件的分發(fā)和傳遞囊骤,更便于我們了解流程:
1.當(dāng)iOS程序中發(fā)生觸摸事件后,系統(tǒng)會(huì)將事件加入到UIApplication管理的一個(gè)任務(wù)隊(duì)列中
2.UIApplication將處于任務(wù)隊(duì)列最前端的事件向下分發(fā)冀值。即UIWindow也物。
3.UIWindow將事件向下分發(fā),即UIView池摧。
4.UIView首先看自己是否能處理事件焦除,觸摸點(diǎn)是否在自己身上激况。如果能作彤,那么繼續(xù)尋找子視圖。
5.遍歷子控件乌逐,重復(fù)以上兩步竭讳。
6.如果沒(méi)有找到,那么自己就是事件處理者浙踢。如果
7.如果自己不能處理绢慢,那么不做任何處理。
其中 UIView不接受事件處理的情況主要有以下三種
1)alpha <0.01
2)userInteractionEnabled = NO
3.hidden = YES.
結(jié)合我們的日志輸出洛波,我們可以看到胰舆,首先調(diào)起的是Widow的hitTest:withEvent:方法,滿(mǎn)足(alpha >=0.01
&& userInteractionEnabled == YES && hidden == NO.) 調(diào)用pointInside:withEvent:方法判斷是否在當(dāng)前響應(yīng)區(qū)域蹬挤,如果在當(dāng)前區(qū)域缚窿,從后往前輪詢(xún)自己的子視圖,調(diào)用子視圖的hitTest:withEvent:方法焰扳,一直找到最合適的響應(yīng)View倦零,返回误续,代碼如下:
// 此方法返回的View是本次點(diǎn)擊事件需要的最佳View
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event{
SGTLog(@" -------- hitTest point %@ Event %@",NSStringFromCGPoint(point),event);
// 1.判斷當(dāng)前控件能否接收事件
if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) return nil;
// 2. 判斷點(diǎn)在不在當(dāng)前控件
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];
// 把當(dāng)前控件上的坐標(biāo)系轉(zhuǎn)換成子控件上的坐標(biāo)系
CGPoint childP = [self convertPoint:point toView:childView];
UIView *fitView = [childView hitTest:childP withEvent:event];
if (fitView) { // 尋找到最合適的view
SGTLog(@" -------- fitView %@",fitView);
return fitView;
}
}
// 循環(huán)結(jié)束,表示沒(méi)有比自己更合適的view
return self;
// return [super hitTest:point withEvent:event];
}
響應(yīng)者鏈及nextResponder
響應(yīng)者鏈
響應(yīng)鏈?zhǔn)菑淖詈线m的view開(kāi)始傳遞,處理事件傳遞給下一個(gè)響應(yīng)者扫茅,響應(yīng)者鏈的傳遞方法是事件傳遞的反方法蹋嵌,如果所有響應(yīng)者都不處理事件,則事件被丟棄葫隙。我們通常用響應(yīng)者鏈來(lái)獲取上幾級(jí)響應(yīng)者栽烂,方法是UIResponder的nextResponder方法。
我們從上邊日志中找到最合適的View是RedView恋脚,所以RedView是響應(yīng)者愕鼓,最終實(shí)現(xiàn)的是RedView的touchesBegan:withEvent:及touchesEnded:withEvent:方法,我們可以在RedView的touchesBegan或者其他方法中處理邏輯慧起,另外如果我們想把事件傳遞到RedView的父視圖菇晃,可以使用nextResponder來(lái)獲取,使用[self.nextResponder touchesBegan:touches withEvent:event];方法可以把響應(yīng)傳遞到下一個(gè)響應(yīng)者(一般為父視圖)
//開(kāi)始觸摸
- (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
我們有時(shí)候可能會(huì)通過(guò)nextResponder來(lái)查找控件的父視圖控件蚓挤。
// 通過(guò)遍歷view上的響應(yīng)鏈來(lái)查找SuperView
UIResponder *responder = self.nextResponder;
while (responder) {
if ([responder isKindOfClass:[SGTBaseView class]]) {
SGTBaseView *superView = (SGTBaseView *)responder;
SGTLog(@" -------- superView %@ ",superView);
break;
}
responder = responder.nextResponder;
}