• 响应者链


    响应者链即一系列响应者对象组成的层次结构。

    理解响应者链需要理清的两个概念(仅以触屏事件Touch Event为例):

    1.First Responder,当前接受触摸的响应者对象,通常是一个UIView对象,作为响应者链的开端。整个响应者链和事件分发的使命就是找出First Responder。

    2.hit-test view,为找出First Responder,系统从上到下(UIWindow->lowest view)检测响应者对象的过程。

    具体的检测流程如下:

    UIWindow实例对象会首先在它的内容视图上调用hitTest:withEvent:,此方法会在其视图层级结构的每个视图上调用pointInside:withEvent:(该方法用来判断点击事件发生的位置是否处于当前视图范围内,以确定用户是不是点击了当前视图),如果pointInside:withEvent:返回YES,则继续逐级调用,直到找到touch操作发生的位置,这个视图就是要找的hit-test view。

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 返回在层级上离当前view最远(离用户最近)且包含指定的point的view。
    
    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 返回boolean值指出receiver是否包含指定的point。
    
    -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
        for (UIView *view in self.subviews) {
            if (!view.hidden && view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event])
                return YES;
        }
        return NO;
    }

    说明:

    1.如果最终hit-test没有找到First Responder,或者First Responder没有处理该事件,则该事件会沿着响应者链向上回溯,如果UIWindow实例和UIApplication实例都不能处理该事件,则该事件被丢弃。

    2.hitTest:withEvent:方法会忽略隐藏(hidden=YES)的视图,禁止用户操作(userInteractionEnabled=YES)的视图,以及alpha级别小于0.01(alpha<0.01)的视图。

    3.可以重写hitTest:withEvent:方法,以达到某些特殊目的,如子视图区域超出父视图的检测(父视图的clipToBounds属性为NO)。

  • 相关阅读:
    模块 hashlib模块
    设计模式
    类中双下方法
    工作小结 常见定制类
    python collections模块
    启动脚本
    anaconda镜像
    理解python的可变参数
    使用spark
    python 异常处理
  • 原文地址:https://www.cnblogs.com/xiaoerheiwatu/p/5807913.html
Copyright © 2020-2023  润新知