主要是記錄下iOS的界面觸摸事件處理機制,而後用一個實例來講明下應用場景.數組
1、處理機制ide
界面響應消息機制分兩塊,(1)首先在視圖的層次結構裏找到能響應消息的那個視圖。(2)而後在找到的視圖裏處理消息。spa
【關鍵】(1)的過程是從父View到子View查找,而(2)是從找到的那個子View往父View回溯(不必定會往回傳遞消息)。對象
1.一、尋找響應消息視圖的過程能夠借用M了個J的一張圖來講明。blog
處理原理以下:繼承
• 當用戶點擊屏幕時,會產生一個觸摸事件,系統會將該事件加入到一個由UIApplication管理的事件隊列中遞歸
• UIApplication會從事件隊列中取出最前面的事件進行分發以便處理,一般,先發送事件給應用程序的主窗口(UIWindow)隊列
• 主窗口會調用hitTest:withEvent:方法在視圖(UIView)層次結構中找到一個最合適的UIView來處理觸摸事件
事件
(hitTest:withEvent:實際上是UIView的一個方法,UIWindow繼承自UIView,所以主窗口UIWindow也是屬於視圖的一種)
it
• hitTest:withEvent:方法大體處理流程是這樣的:
首先調用當前視圖的pointInside:withEvent:方法判斷觸摸點是否在當前視圖內:
▶ 若pointInside:withEvent:方法返回NO,說明觸摸點不在當前視圖內,則當前視圖的hitTest:withEvent:返回nil
▶ 若pointInside:withEvent:方法返回YES,說明觸摸點在當前視圖內,則遍歷當前視圖的全部子視圖(subviews),調用子視圖的hitTest:withEvent:方法重複前面的步驟,子視圖的遍歷順序是從top到bottom,即從subviews數組的末尾向前遍歷,直到有子視圖的hitTest:withEvent:方法返回非空對象或者所有子視圖遍歷完畢:
▷ 若第一次有子視圖的hitTest:withEvent:方法返回非空對象,則當前視圖的hitTest:withEvent:方法就返回此對象,處理結束
▷ 若全部子視圖的hitTest:withEvent:方法都返回nil,則當前視圖的hitTest:withEvent:方法返回當前視圖自身(self)
• 最終,這個觸摸事件交給主窗口的hitTest:withEvent:方法返回的視圖對象去處理。
拿到這個UIView後,就調用該UIView的touches系列方法。
1.二、消息處理過程,在找到的那個視圖裏處理,處理完後根據須要,利用響應鏈nextResponder可將消息往下一個響應者傳遞。
UIAppliactionDelegate <- UIWindow <- UIViewController <- UIView <- UIView
【關鍵】:要理解的有三點:一、iOS判斷哪一個界面能接受消息是從View層級結構的父View向子View傳遞,即樹狀結構的根節點向葉子節點遞歸傳遞。二、hitTest和pointInside成對,且hitTest會調用pointInside。三、iOS的消息處理是,當消息被人處理後默認再也不向父層傳遞。
2、應用實例
【需求】是:界面以下,
Window
-ViewA
-ButtonA
-ViewB
-ButtonB
層次結構:ViewB徹底蓋住了ButtonA,ButtonB在ViewB上,如今須要實現1)ButtonA和ButtonB都能響應消息 2)ViewA也能收到ViewB所收到的touches消息 3)不讓ViewB(ButtonB)收到消息。
(首先解析下,默認狀況下,點擊了ButtonB的區域,iOS消息處理過程。
-ViewA
-ButtonA
-ViewB
-ButtonB
當點擊ButtonB區域後,處理過程:從ViewA開始依次調用hitTest
pointInside的值依次爲:
ViewA:NO;
ViewB:YES;
ButtonB:YES;
ButtonB的subViews:NO;
因此ButtonB的subViews的hitTest都返回nil,因而返回的處理對象是ButtonB本身。接下去開始處理touches系列方法,這裏是調用ButtonB綁定的方法。處理完後消息就中止,整個過程結束。)
【分析】:
實現的方式多種,這裏將兩個需求拆解開來實現,由於實現2就能夠知足1。
2.一、需求1的實現,ViewB蓋住了ButtonA,因此默認狀況下ButtonA收不到消息,可是在消息機制裏尋找消息響應是從父View開始,因此咱們能夠在ViewA的hitTest方法裏作判斷,若touch point是在ButtonA上,則將ButtonA做爲消息處理對象返回。
代碼以下:
#pragma mark - hitTest - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { // 當touch point是在_btn上,則hitTest返回_btn CGPoint btnPointInA = [_btn convertPoint:point fromView:self]; if ([_btn pointInside:btnPointInA withEvent:event]) { return _btn; } // 不然,返回默認處理 return [super hitTest:point withEvent:event]; }
這樣,當觸碰點是在ButtonA上時,則touch消息就被攔截在ViewA上,ViewB就收不到了。而後ButtonA就收到touch消息,會觸發onClick方法。
2.二、需求2的實現,上面說到響應鏈,ViewB只要override掉touches系列的方法,而後在本身處理完後,將消息傳遞給下一個響應者(即父View即ViewA)。
代碼以下:在ViewB代碼裏
#pragma mark - touches - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"B - touchesBeagan.."); // 把事件傳遞下去給父View或包含他的ViewController [self.nextResponder touchesBegan:touches withEvent:event]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"B - touchesCancelled.."); // 把事件傳遞下去給父View或包含他的ViewController [self.nextResponder touchesBegan:touches withEvent:event]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"B - touchesEnded.."); // 把事件傳遞下去給父View或包含他的ViewController [self.nextResponder touchesBegan:touches withEvent:event]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"B - touchesMoved.."); // 把事件傳遞下去給父View或包含他的ViewController [self.nextResponder touchesBegan:touches withEvent:event]; }
而後,在ViewA上就能夠接收到touches消息,在ViewA上寫:
#pragma mark - touches - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"A - touchesBeagan.."); } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"A - touchesCancelled.."); } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"A - touchesEnded.."); } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"A - touchesMoved.."); }
這樣就實現了向父View透傳消息。
2.3 、不讓ViewB收到消息,能夠設置ViewB.UserInteractionEnable=NO;除了這樣還能夠override掉ViewB的ponitInside,原理參考上面。
在ViewB上寫:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { // 本View不響應用戶事件 return NO; }