前言
第一次看到hit-testing的時候是剛學習iOS沒多久,當時覺得這玩意很復雜,便照著官方文檔死記硬背下來了這個方法是干嘛的。
官方文檔
最近在整理筆記的時候看到了響應者鏈又牽扯出了hit-testing的問題,于是便決定自己動手驗證一下hit-testing的調用規則。
下面是官方文檔中對于hit-testing的一個總體概括。
iOS uses hit-testing to find the view that is under a touch. Hit-testing involves checking whether a touch is within the bounds of any relevant view objects. If it is, it recursively checks all of that view’s subviews. The lowest view in the view hierarchy that contains the touch point becomes the hit-test view. After iOS determines the hit-test view, it passes the touch event to that view for handling.
從中我們可以知道hit-testing是調用規則大概是如下所示:
- 先判斷自身的alpha、hidden等屬性,然后通過
pointInside:withEvent:
判斷觸摸點是否在視圖內。 - 如果不在返回nil。否則對視圖的所有子視圖調用hit-testing方法。
- 如果所有子視圖都返回nil則返回自身視圖,否則返回得到的子視圖。
大致流程圖(如果所有子視圖都返回nil則返回自身視圖)
驗證
接下來便是自己驗證該方法的調用規則了。
方法很簡單,重寫UIView的hitTest:withEvent:
的方法,讓這個方法在調用時可以輸出一些區別信息即可。我們可以自定義一個UIView然后重寫他的hitTest:withEvent:
,并且添加一個Name的屬性,然后讓所有view都繼承自這個View來實現。
而在這里為了復習下好久沒用的RunTime,便決定使用RumTime來替換UIView的hitTest:withEvent:
方法以及添加一個屬性。
關于下面的內容可以具體參考下面的博客
南峰子博客
創建一個UIView的分類,重寫他的+ (void)load方法.
+ (void)load {
//替換UIView的hitTest方法的實現
Class cls = [self class];
SEL orignalSelector = @selector(hitTest:withEvent:);
SEL swizzingSelector = @selector(my_hitTest:withEvent:);
//注意不要寫成class_getClassMethod,在這里卡了半天.
Method orignalMethod = class_getInstanceMethod(cls, orignalSelector);
Method swizzingMethod = class_getInstanceMethod(cls, swizzingSelector);
//添加原方法, 但是方法的實現是要替換的后的方法,如果成功只需要把要交換方法的實現替換為原方法的實現就行了, 如果失敗說明類內已有原方法,此時直接交換兩個方法的實現即可.
BOOL success = class_addMethod(cls, orignalSelector, method_getImplementation(swizzingMethod), method_getTypeEncoding(swizzingMethod));
if (!success) {
//這里的方法實現的獲取不能使用class_getImplementation, 因為這里的原方法是已經交換過后的.
method_exchangeImplementations(orignalMethod, swizzingMethod);
} else {
class_replaceMethod(cls, swizzingSelector, method_getImplementation(orignalMethod), method_getTypeEncoding(orignalMethod));
}
}
- (UIView *)my_hitTest:(CGPoint)point withEvent:(UIEvent *)event {
NSString *name = objc_getAssociatedObject(self, &NameKey);
if ([self isMemberOfClass:[UIView class]]) {
NSLog(@"------%@------", name);
}
return [self my_hitTest:point withEvent:event];//此處不會引發循環調用,因為方法已經在runtime中調用過了.
}
再為分類添加一個Name的屬性,因為分類無法添加實例變量,我們需要自己實現,如下.
static const char NameKey;
- (void)setName:(NSString *)name {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 30)];
label.font = [UIFont systemFontOfSize:18];
label.text = name;
label.textColor = [UIColor blackColor];
[self addSubview:label];
objc_setAssociatedObject(self, &NameKey, name, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *)name {
return objc_getAssociatedObject(self, &NameKey);
}
接下來重寫UIViewController的ViewDidLoad方法
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 275, 200)];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 300, 275, 300)];
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 200, 100)];
UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(30, 160, 200, 100)];
view1.backgroundColor = [UIColor yellowColor];
view2.backgroundColor = [UIColor redColor];
view3.backgroundColor = [UIColor greenColor];
view4.backgroundColor = [UIColor greenColor];
[view2 addSubview:view3];
[view2 addSubview:view4];
[self.view addSubview:view1];
[self.view addSubview:view2];
self.view.name = @"main-viewA";
view1.name = @"viewB";
view2.name = @"viewC";
view3.name = @"viewD";
view4.name = @"viewE";
運行如圖
點擊E視圖的時輸出如下(會輸出多次重復結果)
點擊D視圖的時候輸出如下
通過輸出我們便可以發現hit-testing的調用規則的確如上文所說,并且發現他的調用順序是--最后添加的子視圖的hit-testing方法最先被調用。
應用
在知道hit-testing 的具體調用規則之后,我們可以借此做一些事情,當然暫時想不到能做什么很有用的事情。
經過喵神點撥,hit testing的方法在透明判斷的時候用的比較多,比如要點到一個部分透明的overlay的下層的view就可以通過重寫hit-testing來實現。
我們可以通過重寫UIScrollView的hitTest:Event:
方法來實現點擊旁邊空白(圖片中是黑色)處實現滾動,雖然沒有什么卵用。