分享

利用RunTime验证iOS中的hit-testing

 没原创_去搜索 2015-10-09
 

前言

第一次看到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:方法来实现点击旁边空白(图片中是黑色)处实现滚动,虽然没有什么卵用。


END

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多