主要是記錄下iOS的界面觸摸事件處理機制,然后用一個實例來說明下應用場景.
一、處理機制
界面響應消息機制分兩塊,(1)首先在視圖的層次結構里找到能響應消息的那個視圖。(2)然后在找到的視圖里處理消息。
【關鍵】(1)的過程是從父View到子View查找,而(2)是從找到的那個子View往父View回溯(不一定會往回傳遞消息)。
1.1、尋找響應消息視圖的過程可以借用M了個J的一張圖來說明。
處理原理如下:
• 當用戶點擊屏幕時,會產生一個觸摸事件,系統會將該事件加入到一個由UIApplication管理的事件隊列中
• UIApplication會從事件隊列中取出最前面的事件進行分發以便處理,通常,先發送事件給應用程序的主窗口(UIWindow)
• 主窗口會調用hitTest:withEvent:方法在視圖(UIView)層次結構中找到一個最合適的UIView來處理觸摸事件
(hitTest:withEvent:其實是UIView的一個方法,UIWindow繼承自UIView,因此主窗口UIWindow也是屬於視圖的一種)
• 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.2、消息處理過程,在找到的那個視圖里處理,處理完后根據需要,利用響應鏈nextResponder可將消息往下一個響應者傳遞。
UIAppliactionDelegate <- UIWindow <- UIViewController <- UIView <- UIView
【關鍵】:要理解的有三點:1、iOS判斷哪個界面能接受消息是從View層級結構的父View向子View傳遞,即樹狀結構的根節點向葉子節點遞歸傳遞。2、hitTest和pointInside成對,且hitTest會調用pointInside。3、iOS的消息處理是,當消息被人處理后默認不再向父層傳遞。
二、應用實例
【需求】是:界面如下,
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、需求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、需求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; }