iOS 響應(yīng)鏈探索-UIResponder

首先要先了解響應(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;
    }

demo地址

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末磺送,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子灿意,更是在濱河造成了極大的恐慌估灿,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件缤剧,死亡現(xiàn)場(chǎng)離奇詭異馅袁,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)荒辕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén)汗销,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人抵窒,你說(shuō)我怎么就攤上這事弛针。” “怎么了李皇?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵削茁,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我掉房,道長(zhǎng)茧跋,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任卓囚,我火速辦了婚禮瘾杭,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘捍岳。我一直安慰自己富寿,他們只是感情好睬隶,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著页徐,像睡著了一般苏潜。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上变勇,一...
    開(kāi)封第一講書(shū)人閱讀 51,301評(píng)論 1 301
  • 那天恤左,我揣著相機(jī)與錄音,去河邊找鬼搀绣。 笑死飞袋,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的链患。 我是一名探鬼主播巧鸭,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼麻捻!你這毒婦竟也來(lái)了纲仍?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤贸毕,失蹤者是張志新(化名)和其女友劉穎郑叠,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體明棍,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡乡革,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了摊腋。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片沸版。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖歌豺,靈堂內(nèi)的尸體忽然破棺而出推穷,到底是詐尸還是另有隱情心包,我是刑警寧澤类咧,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站蟹腾,受9級(jí)特大地震影響痕惋,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜娃殖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一值戳、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧炉爆,春花似錦堕虹、人聲如沸卧晓。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)逼裆。三九已至,卻和暖如春赦政,著一層夾襖步出監(jiān)牢的瞬間胜宇,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工恢着, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留桐愉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓掰派,卻偏偏與公主長(zhǎng)得像从诲,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子靡羡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容