假設當前有 ViewA 與 ViewB,如果不重寫 hitTest:withEvent:,系統會先呼叫 ViewA 的 hitTest,再往下找 ViewB。系統內部的處理流程,跟下面這段重寫程式碼幾乎一模一樣:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if ([self pointInside:point withEvent:event]) {
// 觸摸點在自己身上,繼續往子視圖找
} else {
return nil;
}
for (UIView *subView in self.subviews) {
if ([subView hitTest:point withEvent:event] != nil) {
return subView;
}
}
return self;
}
也就是說,不重寫時,每個 UIView 的預設行為就是上面這段邏輯。一般情況這樣就夠用了。
重寫 hitTest 最常見的目的,是穿透上層的 view,讓 touch 事件能達到下面的 view。
例如 ViewB 完全蓋住了 ViewA,但我們希望點擊 ViewB 時由 ViewA 來響應,可以這樣寫:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if ([self pointInside:point withEvent:event]) {
NSLog(@"in view A");
return self;
}
return nil;
}
現在有一個更實際的界面結構:
Window
|- ViewA
| |- ButtonA
|- ViewB
|- ButtonB
層次關係是:ViewB 完全蓋住了 ButtonA,ButtonB 疊在 ViewB 之上。需求如下:
先理解一下預設情況下點擊 ButtonB 區域時 iOS 的處理過程。從 ViewA 開始依次呼叫 hitTest,各層 pointInside: 的回傳值為:
ViewA : NO ViewB : YES ButtonB : YES ButtonB.subViews : NO
於是 ButtonB 的子視圖 hitTest 都回傳 nil,最終由 ButtonB 自己作為處理物件,接著進入 touches 系列方法,觸發 ButtonB 綁定的 action,訊息傳遞到此結束。
ViewB 蓋住了 ButtonA,預設情況下 ButtonA 收不到訊息。但既然查找響應物件是從父視圖開始,我們就可以在 ViewA 的 hitTest 中做判斷:如果觸摸點落在 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 則能正常觸發它的點擊方法。
ViewB 只要覆寫 touches 系列方法,在自己處理完之後,透過 nextResponder 把訊息交給下一個響應者(即父視圖 ViewA):
// 在 ViewB 中
#pragma mark - touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"B - touchesBegan..");
// 把事件傳遞下去給父 View 或包含它的 ViewController
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"B - touchesMoved..");
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"B - touchesEnded..");
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"B - touchesCancelled..");
[self.nextResponder touchesBegan:touches withEvent:event];
}
然後在 ViewA 中就能收到這些訊息:
// 在 ViewA 中
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"A - touchesBegan..");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"A - touchesMoved..");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"A - touchesEnded..");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"A - touchesCancelled..");
}
最直接的方式是設定 ViewB.userInteractionEnabled = NO;。除此之外,也可以覆寫 pointInside:withEvent: 直接回傳 NO:
// 在 ViewB 中
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
// 本 View 不響應用戶事件
return NO;
}
三個需求合起來看,其實說明了同一件事:hitTest:withEvent: 決定「誰來處理」,而 touches 系列方法搭配 nextResponder 決定「處理不完就往誰傳」。把這兩層機制分清楚,絕大多數看似詭異的點擊穿透、事件被父層吃掉等問題都能找到明確的解法。