美文网首页
iOS事件层次分析

iOS事件层次分析

作者: 心亦逸风 | 来源:发表于2018-07-14 16:19 被阅读11次

1.事件种类

触摸事件:通过触摸、手势进行触发
运动事件(加速事件):通过手机加速计触发(如晃动手机)
远程控制事件:通过其它设备触发(如耳机控制音量)

2.app从系统获取事件

app启动时在创建的事件循环Runloop中注册了一个Source1来接收系统事件。当接收到系统事件(触摸/摇晃/锁屏等)后,回调给Source0,Source0通过调用_UIApplicationHandleEventQueue()把事件包装成UIEvent进行处理或分发。
其中包括识别 UIGesture/处理屏幕旋转/发送给 UIWindow 等。通常事件如UIButton点击、touchesBegin/Move/End/Cancel 事件。
下面主要讲触摸事件的处理响应流程。

3.事件传递和响应流程

  • 首先通过下面两个方法找到触摸到的最上面的view(这两个方法在UIView中):
//判断触摸事件的点point是否在view上
- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event;

//大致流程是先判断触摸事件是否在自己view上如果是再循环调用subview的hitTest方法
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event;

//hitTest大致处理逻辑代码
- (UIView *)eocHitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.alpha <= 0.01 || !self.userInteractionEnabled || self.hidden) {
        return nil;
    }
    
    if ([self pointInside:point withEvent:event]) {  //发生在我的范围内
        //遍历子view
        NSArray *subViews = [[self.subviews reverseObjectEnumerator] allObjects];
        UIView *tmpView;
        
        for (UIView *subView in subViews) {
            //转换坐标系,然后判断该点是否在bounds范围内
            CGPoint convertedPoint = [self convertPoint:point toView:subView];
            tmpView = [subView eocHitTest:convertedPoint withEvent:event];
        }
        return tmpView?tmpView:self;
        
    } else {
        return nil;
    }
       
}
触摸事件传递流程
  • 找到相应View后会执行它自身四个点击方法:
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;//手势事件开始时执行
    - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;//手势事件移动时执行
    - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;//手势事件结束时执行
    - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event//手势事件被取消时
    UIView继承自UIResponder,所有的View上都会一个nextResponder属性来记录下一个需要相应的View(一般是它的superview),viewController的view的nextResponder是viewController本身,如果vc在UINavigationController中,那vc的nextResponder就是navigationController。
    根据nextRepsonder连接,构成了事件响应链,从最末端的view开始执行四个事件方法,直到顶部或者手势事件被取消。一般按钮(button)和view上添加的手势事件(UIGestureRecognizer)都是通过自身的四个事件方法判断的。
    nextResponder响应链

4.代码测试

#import "FindingViewViewController.h"
#import "TestView.h"

@interface FindingViewViewController ()
{
    TestView *whiteView;
}

@end

@implementation FindingViewViewController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    NSLog(@"%@",self.nextResponder);
}

- (void)loadView
{
    whiteView = [[TestView alloc] initWithFrame:[UIScreen mainScreen].bounds color:[UIColor whiteColor]];
    self.view = whiteView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"hitTest/pointInside寻找view";
    
    [self createViewHierachy];
}

- (void)createViewHierachy {
    
    TestView *grayView = [[TestView alloc] initWithFrame:CGRectMake(50.f, 100.f, 260.f, 200.f) color:[UIColor grayColor]];
    
    TestView *redView = [[TestView alloc] initWithFrame:CGRectMake(0.f, 0.f, 120.f, 100.f) color:[UIColor redColor]];
    
    TestView *blueView = [[TestView alloc] initWithFrame:CGRectMake(80.f, 60.f, 100.f, 100.f) color:[UIColor blueColor]];
    
    TestView *yellowView = [[TestView alloc] initWithFrame:CGRectMake(50.f, 360.f, 200.f, 200.f) color:[UIColor yellowColor]];
    
    NSLog(@"%@",blueView.nextResponder.description);
    NSLog(@"%@",redView.nextResponder.description);
    NSLog(@"%@",yellowView.nextResponder.description);
    NSLog(@"%@",grayView.nextResponder.description);
    
    [self.view addSubview:grayView];
    [grayView addSubview:redView];
    [grayView addSubview:blueView];
    [self.view addSubview:yellowView];
    UITapGestureRecognizer
    NSLog(@"%@",blueView.nextResponder.description);
    NSLog(@"%@",redView.nextResponder.description);
    NSLog(@"%@",yellowView.nextResponder.description);
    NSLog(@"%@",grayView.nextResponder.description);
    NSLog(@"%@",whiteView.nextResponder.description);
    NSLog(@"%@",self.nextResponder.description);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    NSLog(@"%@ touchBegan", self.description);
    [super touchesBegan:touches withEvent:event];
    
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    NSLog(@"%@ touchesMoved", self.description);
    [super touchesMoved:touches withEvent:event];
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    NSLog(@"%@ touchesEnded", self.description);
    [super touchesEnded:touches withEvent:event];
    
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    NSLog(@"%@ touchesCancelled", self.description);
    [super touchesCancelled:touches withEvent:event];
    
}

当我在红蓝相间的地方滑动时的打印:

2018-07-14 16:12:33.144920+0800 IOS事件层次分析和事件冲突处理[22934:1036469] whiteView -[TestView hitTest:withEvent:]
2018-07-14 16:12:33.145177+0800 IOS事件层次分析和事件冲突处理[22934:1036469] whiteView pointInside
2018-07-14 16:12:33.145412+0800 IOS事件层次分析和事件冲突处理[22934:1036469] yellowView -[TestView hitTest:withEvent:]
2018-07-14 16:12:33.145574+0800 IOS事件层次分析和事件冲突处理[22934:1036469] yellowView pointInside
2018-07-14 16:12:33.145764+0800 IOS事件层次分析和事件冲突处理[22934:1036469] grayView -[TestView hitTest:withEvent:]
2018-07-14 16:12:33.145936+0800 IOS事件层次分析和事件冲突处理[22934:1036469] grayView pointInside
2018-07-14 16:12:33.146127+0800 IOS事件层次分析和事件冲突处理[22934:1036469] blueView -[TestView hitTest:withEvent:]
2018-07-14 16:12:33.146314+0800 IOS事件层次分析和事件冲突处理[22934:1036469] blueView pointInside

2018-07-14 16:12:33.149301+0800 IOS事件层次分析和事件冲突处理[22934:1036469] blueView touchBegan
2018-07-14 16:12:33.149497+0800 IOS事件层次分析和事件冲突处理[22934:1036469] grayView touchBegan
2018-07-14 16:12:33.149671+0800 IOS事件层次分析和事件冲突处理[22934:1036469] whiteView touchBegan
2018-07-14 16:12:33.149851+0800 IOS事件层次分析和事件冲突处理[22934:1036469] <FindingViewViewController: 0x7f86d2234360> touchBegan
2018-07-14 16:12:33.234643+0800 IOS事件层次分析和事件冲突处理[22934:1036469] blueView touchesEnded
2018-07-14 16:12:33.234921+0800 IOS事件层次分析和事件冲突处理[22934:1036469] grayView touchesEnded
2018-07-14 16:12:33.235119+0800 IOS事件层次分析和事件冲突处理[22934:1036469] whiteView touchesEnded
2018-07-14 16:12:33.235263+0800 IOS事件层次分析和事件冲突处理[22934:1036469] <FindingViewViewController: 0x7f86d2234360> touchesEnded
2018-07-14 16:12:45.114598+0800 IOS事件层次分析和事件冲突处理[22934:1036469] whiteView -[TestView hitTest:withEvent:]
2018-07-14 16:12:45.114774+0800 IOS事件层次分析和事件冲突处理[22934:1036469] whiteView pointInside
2018-07-14 16:12:45.114896+0800 IOS事件层次分析和事件冲突处理[22934:1036469] yellowView -[TestView hitTest:withEvent:]
2018-07-14 16:12:45.114987+0800 IOS事件层次分析和事件冲突处理[22934:1036469] yellowView pointInside
2018-07-14 16:12:45.115115+0800 IOS事件层次分析和事件冲突处理[22934:1036469] grayView -[TestView hitTest:withEvent:]
2018-07-14 16:12:45.115249+0800 IOS事件层次分析和事件冲突处理[22934:1036469] grayView pointInside
2018-07-14 16:12:45.115381+0800 IOS事件层次分析和事件冲突处理[22934:1036469] blueView -[TestView hitTest:withEvent:]
2018-07-14 16:12:45.115494+0800 IOS事件层次分析和事件冲突处理[22934:1036469] blueView pointInside
2018-07-14 16:12:45.115741+0800 IOS事件层次分析和事件冲突处理[22934:1036469] whiteView -[TestView hitTest:withEvent:]
2018-07-14 16:12:45.115873+0800 IOS事件层次分析和事件冲突处理[22934:1036469] whiteView pointInside
2018-07-14 16:12:45.218887+0800 IOS事件层次分析和事件冲突处理[22934:1036469] yellowView -[TestView hitTest:withEvent:]
2018-07-14 16:12:45.219027+0800 IOS事件层次分析和事件冲突处理[22934:1036469] yellowView pointInside
2018-07-14 16:12:45.219156+0800 IOS事件层次分析和事件冲突处理[22934:1036469] grayView -[TestView hitTest:withEvent:]
2018-07-14 16:12:45.219273+0800 IOS事件层次分析和事件冲突处理[22934:1036469] grayView pointInside
2018-07-14 16:12:45.219368+0800 IOS事件层次分析和事件冲突处理[22934:1036469] blueView -[TestView hitTest:withEvent:]
2018-07-14 16:12:45.219489+0800 IOS事件层次分析和事件冲突处理[22934:1036469] blueView pointInside
2018-07-14 16:12:45.220157+0800 IOS事件层次分析和事件冲突处理[22934:1036469] blueView touchBegan
2018-07-14 16:12:45.220361+0800 IOS事件层次分析和事件冲突处理[22934:1036469] grayView touchBegan
2018-07-14 16:12:45.220490+0800 IOS事件层次分析和事件冲突处理[22934:1036469] whiteView touchBegan
2018-07-14 16:12:45.220623+0800 IOS事件层次分析和事件冲突处理[22934:1036469] <FindingViewViewController: 0x7f86d2234360> touchBegan
2018-07-14 16:12:45.221186+0800 IOS事件层次分析和事件冲突处理[22934:1036469] blueView touchesMoved
2018-07-14 16:12:45.221302+0800 IOS事件层次分析和事件冲突处理[22934:1036469] grayView touchesMoved
2018-07-14 16:12:45.221402+0800 IOS事件层次分析和事件冲突处理[22934:1036469] whiteView touchesMoved
2018-07-14 16:12:45.221526+0800 IOS事件层次分析和事件冲突处理[22934:1036469] <FindingViewViewController: 0x7f86d2234360> touchesMoved
2018-07-14 16:12:45.226950+0800 IOS事件层次分析和事件冲突处理[22934:1036469] blueView touchesMoved
2018-07-14 16:12:45.227171+0800 IOS事件层次分析和事件冲突处理[22934:1036469] grayView touchesMoved
2018-07-14 16:12:45.227335+0800 IOS事件层次分析和事件冲突处理[22934:1036469] whiteView touchesMoved
2018-07-14 16:12:45.227492+0800 IOS事件层次分析和事件冲突处理[22934:1036469] <FindingViewViewController: 0x7f86d2234360> touchesMoved
2018-07-14 16:12:45.243216+0800 IOS事件层次分析和事件冲突处理[22934:1036469] blueView touchesMoved
2018-07-14 16:12:45.243463+0800 IOS事件层次分析和事件冲突处理[22934:1036469] grayView touchesMoved
2018-07-14 16:12:45.243612+0800 IOS事件层次分析和事件冲突处理[22934:1036469] whiteView touchesMoved
2018-07-14 16:12:45.243720+0800 IOS事件层次分析和事件冲突处理[22934:1036469] <FindingViewViewController: 0x7f86d2234360> touchesMoved
2018-07-14 16:12:45.263682+0800 IOS事件层次分析和事件冲突处理[22934:1036469] blueView touchesMoved
2018-07-14 16:12:45.263803+0800 IOS事件层次分析和事件冲突处理[22934:1036469] grayView touchesMoved
2018-07-14 16:12:45.263913+0800 IOS事件层次分析和事件冲突处理[22934:1036469] whiteView touchesMoved
2018-07-14 16:12:45.264028+0800 IOS事件层次分析和事件冲突处理[22934:1036469] <FindingViewViewController: 0x7f86d2234360> touchesMoved
2018-07-14 16:12:45.376834+0800 IOS事件层次分析和事件冲突处理[22934:1036469] blueView touchesEnded
2018-07-14 16:12:45.377081+0800 IOS事件层次分析和事件冲突处理[22934:1036469] grayView touchesEnded
2018-07-14 16:12:45.377272+0800 IOS事件层次分析和事件冲突处理[22934:1036469] whiteView touchesEnded
2018-07-14 16:12:45.377474+0800 IOS事件层次分析和事件冲突处理[22934:1036469] <FindingViewViewController: 0x7f86d2234360> touchesEnded

最后时测试代码IOSStudyTest

相关文章

网友评论

      本文标题:iOS事件层次分析

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