美文网首页
iOS事件处理和响应链

iOS事件处理和响应链

作者: 佟小胆胆小 | 来源:发表于2018-05-29 22:06 被阅读0次

    说到iOS中的触摸事件不得不提到两个内容:一个是UIResponder、一个是事件的响应链

    什么UIResponser?

    Responsder objects是UIResponder的实例化对象,他组成了事件处理的骨架,许多关键对象都是UIResponder对象包括UIApplication、UIViewController、UIView也包括UIWindow。
    也就是说只要继承UIResponder的类都可以相应事件。

    一般我们通过重写UIResponder的四个方法来处理事件

    // 一根或者多根手指开始触摸view
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    // 一根或者多根手指在view上移动,【会多次调用】
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    // 一根或者多根手指离开view
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    // 触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    // 提示:touches中存放的都是UITouch对象
    

    响应链

    一个触摸事件是从用户点击屏幕开始 由UIApplication向下分发 经由 UIWindow 在向下传递给UIViewController和UIVIew
    那么 我们一个UIViewController有那么多View到底事件应该交给谁消费掉呢?
    有两个重要的方法

    • hitTest:withEvent:
    • pointInside:withEvent:

    他们的作用分别是
    hitTest:withEvent:寻找并返回最合适的view、
    pointInside:withEvent: 判断点是否在当前的View上

    • 两个方法调用顺序
      我们通过实例验证一下 新建四个View并重写他们的touchesXXX方法、hitTest:withEvent: 方法、以及pointInside:withEvent:方法并打印方法名和类名


      views
    //示例 其他View以此为模板
    #import "GrayView.h"
    
    @implementation GrayView
    
    - (void)layoutSubviews
    {
        self.backgroundColor = [UIColor grayColor];
    }
    
    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    {
        NSLog(@"%@ pointInside", NSStringFromClass([self class]));
        return [super pointInside:point withEvent:event];
    }
    
    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    {
        
        NSLog(@"%@ hitTest", NSStringFromClass([self class]));
        
        return [super hitTest:point withEvent:event];
        
    }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        
        NSLog(@"%@ touchBegan", NSStringFromClass([self class]));
        [super touchesBegan:touches withEvent:event];
    }
    
    - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"%@ touchesMoved",NSStringFromClass([self class]));
        [super touchesMoved:touches withEvent:event];
    }
    
    - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"%@ touchesEnded", NSStringFromClass([self class]));
        [super touchesEnded:touches withEvent:event];
    }
    
    - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"%@ touchesCancelled", NSStringFromClass([self class]));
        [super touchesCancelled:touches withEvent:event];
    }
    
    @end
    
    //在ViewController中添加他们
    //ViewController.m
    GrayView *grayView = [[GrayView alloc] initWithFrame:CGRectMake(50.f, 100.f, 260.f, 200.f)];
        
        RedView *redView = [[RedView alloc] initWithFrame:CGRectMake(0.f, 0.f, 120.f, 100.f)];
        
        BlueView *blueView = [[BlueView alloc] initWithFrame:CGRectMake(140.f, 100.f, 100.f, 100.f)];
        
        YellowView *yellowView = [[YellowView alloc] initWithFrame:CGRectMake(50.f, 360.f, 200.f, 200.f)];
        
        [self.view addSubview:grayView];
        [grayView addSubview:redView];
        [grayView addSubview:blueView];
        [self.view addSubview:yellowView];
    

    运行后的效果图


    运行图

    当我们点击灰色View时,控制台打印如下信息


    console
    我们看到最先调用了YellowView的hitTest方法而后调用pointInside方法,如果pointInside返回NO说明这个触摸点不在这个View上就遍历跟他同一级的下一个View,如果pointInside返回YES说明触摸点在这个View的范围内,则逆序遍历他的子View,如果子View的pointInside都返回NO那么说明这个触摸点在当前返回YES的这个View上

    有几点回影响hitTest流程当

    • alpha<=0.01
    • userInterActionEnabled = NO
    • hidden = YES;
      pointInside都会返回NO

    我们可以根据上面的结论自己写一下hitTest的内部逻辑验证推论是否正确

    //GrayView.m
    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    {
       
        NSLog(@"%s",__func__);
        //逆序遍历子VIew看触摸点是否在子View中
        NSArray *subViews = [[self.subviews reverseObjectEnumerator] allObjects];
        UIView *tmpView = nil;
        for (UIView *view in subViews) {
            
            CGPoint convertedPoint = [self convertPoint:point toView:view];
            if ([view pointInside:convertedPoint withEvent:event]) {
                
                tmpView = view;
                break;
                
            }
            
        }
        
      
        
        if (tmpView) {
        //在子View中
            return tmpView;
        } else if([self pointInside:point withEvent:event]) {
            //点不在子View中而在自己身上
            return self;
        } else {
            //触摸点不在当前View上
            return nil;
            
        }
        
    
    }
    

    我们依然点击GrayView发现依然可以找到正确被点击的View


    console

    现在我们给RedView的hitTest方法打一个断点看并点击RedView查看调用层级


    responder_chain

    相关文章

      网友评论

          本文标题:iOS事件处理和响应链

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