1. UIResponse 事件响应基础篇

1. 事件的传递

先要介绍一下事件传递 , 简单的就是 手机端捕捉到一个事件 =》 传递给App =》 Window =》 Controller =》 View =》 子视图 ; 一般通过 hitTest:withEventpointInside:withEvent 来查找,这个过程比较简单,网上有很多;但是网上对于事件的响应的介绍确很少;

2. 事件的响应

就是 通过 touchesBegan:withEvent: , touchesMoved:withEvent, touchesCancelled:withEvent,touchesEnded:withEvent 来进行事件的接收和处理;

3. 通过一个简单的例子 来看看

  1. 定义 一个 子视图 MyView;
  2. 父视图 Controller的 self.view 来承载MyView
@implementation MyView


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"MyView touchesBegan");

    // 触发 parentView的 touchBegan
    [super touchesBegan:touches withEvent:event];
    
    NSLog(@"MyView touchesBegan--2");

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"MyView touchesMoved");

    [super touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"MyView touchesCancelled");

    [super touchesCancelled:touches withEvent:event];
    
    NSLog(@"MyView touchesCancelled-2");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"MyView touchesEnded");

    [super touchesEnded:touches withEvent:event];
    
    NSLog(@"MyView touchesEnded——2");

}
@end

并且在 Controller 也添加

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"self.view touchesBegan");

    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"self.view touchesMoved");

    [super touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"self.view touchesCancelled");

    [super touchesCancelled:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"self.view touchesEnded");

    [super touchesEnded:touches withEvent:event];
}

通过点击 MyView 我大概得出一些简单的结论

3.1 正常的触发

说明先触发 最上面的 子视图 touchesBegan =》 parentView的 touchBegan =》 子视图 touchesBegan 结束;

MyView touchesBegan
self.view touchesBegan
MyView touchesBegan--2

MyView touchesEnded
self.view touchesEnded
MyView touchesEnded——2

3.2 注释掉 MyView 里面的 [super touchesBegan:touches withEvent:event]; 和 [super touchesEnded:touches withEvent:event];

父类的 touchBegan 和 touchesEnd 没有调用

MyView touchesBegan
MyView touchesBegan--2
MyView touchesEnded
MyView touchesEnded——2

3.3 MyView 中 添加手势
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)tapClick {
    
    NSLog(@"MyView tapClick");
}

会直接 touchesCancelled 掉,然后去执行手势了;在 https://developer.apple.com/documentation/uikit/uigesturerecognizer 中介绍了,手势 的优先级 要比 UIRespons 要高,所以当遇到手势,UIResponse 直接被canceled;
A window delivers touch events to a gesture recognizer before it delivers them to the hit-tested view attached to the gesture recognizer. Generally, if a gesture recognizer analyzes the stream of touches in a multi-touch sequence and doesn’t recognize its gesture, the view receives the full complement of touches. If a gesture recognizer recognizes its gesture, the remaining touches for the view are cancelled

MyView touchesBegan
self.view touchesBegan
MyView touchesBegan--2

// 手势响应
MyView tapClick

// 会进入cancelled的响应
MyView touchesCancelled
self.view touchesCancelled
MyView touchesCancelled-2

你可能感兴趣的:(1. UIResponse 事件响应基础篇)