iOS事件传递实际应用

作者: 熊猫人和熊猫君 | 来源:发表于2017-05-26 20:57 被阅读0次

    老掉牙之iOS事件传第一部份 从下至上

    当用户点击了界面,首先做出反应的肯定是手机硬件了,然后才是操作系统iOS,iOS通过事件队列分发到我们点击的APP。而接下来的就是下面两个函数。

    - (UIView )hitTest:(CGPoint)point withEvent:(UIEvent )event
    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    

    iOS 中事件传递分为两个部分:
    1.事件传递第一部份 从下至上 ,iOS -> [window hitTest:withEvent]-> find touched viewA -> touch.view =viewA -> [application sendEvent:event]->touchesBegin:withEvents;
    2.事件传递第二部份 从上至下 ,事件链。

    事件传递第一部分就定位到了界面最顶端,如你点击的一个按钮,但是有可能这个按钮有可能不想响应事件,想交给下面的响应者响应,这就是事件传递的第二部份了,总有来说相当于跑了个来回,第一部分是开始跑跑到了终点(你点击的顶层界面元素),然后折返跑;利用的工具分别是hitTest pointInside组合,事件链。

    hitTest内部实现原理

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
        if (!self.isUserInteractionEnabled || self.isHidden || self.alpha <= 0.01) {
            return nil;
        }
        if ([self pointInside:point withEvent:event]) {
            for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
                CGPoint convertedPoint = [subview convertPoint:point fromView:self];
                UIView *hitTestView = [subview hitTest:convertedPoint withEvent:event];
                if (hitTestView) {
                    return hitTestView;
                }
            }
            return self;
        }
        return nil;
    }
    

    备注:
    看懂此算法需要了解图形树,不懂可以纸上画一画,比较简单,算法主要过程为,pointInside快速定位落点区域,深度优先递归落点区域的子孙后代,子孙后代中找到终止查找,否则返回自己。

    hitTest的实战:

    需求1:
    当在一个view上添加一个屏蔽罩,但又不影响对下面view的操作,也就是可以透过屏蔽罩对下面的view进行操作。

    在屏蔽罩类中增加如下函数

    - (id)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
    {
          UIView *hitView = [super hitTest:point withEvent:event];
          if (hitView == self)
          {
              return nil;
           }
          else 
          {
            return hitView;
          }
    }
    

    需求2:
    覆盖类下的被覆盖者需响应事件,如响应一个scrollview下面的按钮点击,但scrollview自身可用

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
     {
          CGPoint hitPoint = [_underButton convertPoint:point fromView:self];
          if (_underButton pointInside:hitPoint withEvent:event]) 
        return _underButton;
        return [super hitTest:point withEvent:event];
    }
    

    当然也可以用pointInside,从前面hitTest的实现原理可知,hitTest在遍历过程中是以pointInside作为判断条件之一确定是否继续遍历。在系统中pointInside底层是识别点击是否落在自身区域内。

    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    {
        CGPoint _underButton = [self convertPoint:point toView:_underButton];
        BOOL flag = [_underButton pointInside:_underButton withEvent:event];
        if (flag) {
            return NO;
        }
        return [super pointInside:point withEvent:event];
    }
    

    需求3:
    利用hitTest扩大一个按钮的点击区域,当点击按钮周边的时候也相当于点击了按钮本身

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
        if (!self.isUserInteractionEnabled || self.isHidden || self.alpha <= 0.01) {
            return nil;
        }
        CGRect touchRect = CGRectInset(self.bounds, -10, -10);
        if (CGRectContainsPoint(touchRect, point)) {
            for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
                CGPoint convertedPoint = [subview convertPoint:point fromView:self];
                UIView *hitTestView = [subview hitTest:convertedPoint withEvent:event];
                if (hitTestView) {
                    return hitTestView;
                }
            }
            return self;
        }
        return nil;
    }
    

    分析:
    可以看hitTest实现原理,是一个深度优先的遍历算法,当你点击按钮外部的时候,如按钮父viewFather不响应返回hitTest,根据深度优先最先找到viewFather的子孙中最TOP的view也就是深度最深的View,这里也就是按钮本身了,而按钮本身我们已经重载了hitTest,这时重载的按钮hitTest将执行,如上算法,按钮外围10个点的区域点击都转到按钮本身(假设按钮自身无subviews),如此点击按钮外围也能触发按钮点击事件。
    https://juejin.im/post/5b614924f265da0f774ac7be

    相关文章

      网友评论

        本文标题:iOS事件传递实际应用

        本文链接:https://www.haomeiwen.com/subject/fqgxfxtx.html