標籤:
主要是記錄下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:方法返回非Null 物件或者全部子視圖遍曆完畢:
? 若第一次有子視圖的hitTest:withEvent:方法返回非Null 物件,則當前視圖的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作為訊息處理對象返回。
代碼如下:
| 12345678910111213 |
#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代碼裡
| 123456789101112131415161718192021222324252627282930 |
#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上寫:
| 123456789101112131415161718192021 |
#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上寫:
| 123456 |
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{ // 本View不響應使用者事件 return NO; } |
iOS觸摸事件處理--備用