iOSiios开发-事件分发机制(hitTest与响应链)

2022-04-12  本文已影响0人  Edviin_2de8
一、什么是hitTest

hitTest: withEvent: 是UIView 里面的一个方法,该方法的作用 在于 : 在视图的层次结构中寻找一个最适合的 view 来响应触摸事件。

该方法会被系统调用,调用的时候,如果返回为nil,即事件有可能被丢弃,否则返回最合适的view 来响应事件。

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

二、hitTest 的调用顺序

touch -> UIApplication -> UIWindow -> UIViewController.view -> subViews -> ....-> 合适的view
1

三、事件的传递顺序

事件传递顺序与hitTest 的调用顺序 恰好相反
view -> superView ...- > UIViewController.view -> UIViewController -> UIWindow -> UIApplication -> 事件丢弃

  1. 首先由 view 来尝试处理事件,如果他处理不了,事件将被传递到他的父视图superview
  2. superview 也尝试来处理事件,如果他处理不了,继续传递他的父视图 UIViewcontroller.view
  3. UIViewController.view尝试来处理该事件,如果处理不了,将把该事件传递给UIViewController
  4. UIViewController尝试处理该事件,如果处理不了,将把该事件传递给主窗口Window
    5.主窗口Window尝试来处理该事件,如果处理不了,将传递给应用单例Application
    6.如果Application也处理不了,则该事件将会被丢弃
四、hitTest的实现思路

常见的视图不响应事件不外乎如下几种情况

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    // 如果交互未打开,或者透明度小于0.05 或者 视图被隐藏
    if (self.userInteractionEnabled == NO || self.alpha < 0.05 || self.hidden == YES)
    {
        return nil;
    }
    // 如果 touch 的point 在 self 的bounds 内
    if ([self pointInside:point withEvent:event])
    {
        for (UIView *subView in self.subviews)
        {
            //进行坐标转化
            CGPoint coverPoint = [subView convertPoint:point fromView:self];

           // 调用子视图的 hitTest 重复上面的步骤。找到了,返回hitTest view ,没找到返回有自身处理
            UIView *hitTestView = [subView hitTest:coverPoint withEvent:event];
            if (hitTestView)
            {
                return hitTestView;
            }
        }
        return self;
    }
    return nil;
}

五、实践场景

首先我们通过代码创建一个具有层次结构的视图集合,在viewcontroller的viewDidLoad中添加如下代码:

    greenView *green = [[greenView alloc] initWithFrame:CGRectMake(50, 50, 300, 500)];
    [self.view addSubview:green];
    
    redView *red = [[redView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];
    [green addSubview:red];
    
    orangeView *orange = [[orangeView alloc] initWithFrame:CGRectMake(0, 350, 200, 100)];
    [green addSubview:orange];
    
    blueView *blue = [[blueView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    [red addSubview:blue];
image.png

即响应链的第一响应者,也可以通过重写touches方法来决定该由响应链上的谁来响应事件。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) return nil;
    if ([self pointInside:point withEvent:event] == NO) return nil;
    //红色视图是先被add的,所以是第一个元素
    return self.subviews[0];
}

我们这里是重写了父视图的hitTest方法,而不是重写红色视图的hitTest方法并让它返回自身,道理也很显然,在遍历绿色视图所有子视图的过程中,可能还没来得及调用到红色视图的hitTest方法时,就已经遍历到了触摸点真正所在的绿色视图,这个时候重写红色视图的hitTest方法是无效的。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    return nil;
}

当然,我们也可以重写绿色视图的hitTest方法,让其直接返回自身,也能实现同样效果,不过这样的话点击其它子视图(比如黄色视图)就也不能响应事件了,因此如何处理需要视情况而定。

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"red touches begin");  //自己的处理
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"red touches moved");  //自己的处理
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"red touches end");  //自己的处理
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"red touches canceled");  //自己的处理
    [super touchesBegan:touches withEvent:event];
}

需要说明的是,事件传递给下一个响应者时,用的是super而不是superview,这并没有问题,因为super调用了父类的实现,而父类默认的实现就是调用下一个响应者的touches方法。如果直接调用superview反而会有问题,因为下一个响应者可能是viewcontroller

上一篇 下一篇

猜你喜欢

热点阅读