iOS事件响应链

作者: 风与鸾 | 来源:发表于2017-06-30 15:48 被阅读25次

前言

当用户点击付款跳转到付款界面、点击扫一扫进入扫描二维码视图。
当我们点击屏幕的时候,这个点击事件由硬件层传向iPhone OS(操作系统),
然后操作系统把这些信息包装成UITouch(点击对象)和UIEvent(事件对象),然后找到当前运行的程序,
逐级寻找能够响应这次事件的响应者,这一寻找过程称为事件的响应链。
过程:AppDelegate -> UIApplication -> UIWindow ->UIController(UIResponder子类)-UIView->subViews

  • UITouch
    UITouch表示单个点击,其类文件中存在枚举类型UITouchPhase的属性,用来表示当前点击的状态。
    这些状态包括点击开始、移动、停止不动、结束和取消五个状态。
    每次点击发生的时候,点击对象都放在一个集合中传入UIResponder的回调方法中。
    我们通过集合中对象获取用户点击的位置,其中通过- (CGPoint)locationInView:(nullable UIView *)view获取当前点击坐标点,
    - (CGPoint)previousLocationInView:(nullable UIView *)view获取上个点击位置的坐标点。
  • UIEvent
    iOS使用UIEvent表示用户交互的事件对象,在UIEvent.h文件中,我们可以看到有一个UIEventType类型的属性;
    这个属性表示了当前的响应事件类型;分别有多点触控、摇一摇以及新增的3DTouch事件类型。
    在一个用户点击事件处理过程中,UIEvent对象是唯一的。
查找和响应流程.jpg

Hit-test View

决定谁是Hit-test View通过不断递归- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event以下两个函数实现的:

// recursively calls -pointInside:withEvent:. point is in the receiver's coordinate system
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;

// default returns YES if point is in bounds
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;

Hit-test检查机制

  • 通过调用自身- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event如果返回YES,则继续遍历View中subViews直到没有subViews为止。
  • 如果当前View.userInteractionEnabled = NO,enabled=NO(UIControl),或者alpha<=0.01, View Hidden等情况的时候,hitTest就不会调用自己的- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event了,直接返回nil,然后系统去遍历其他子节点。
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
   if (self.alpha <= 0.01 || !self.userInteractionEnabled || self.hidden) { 
      return nil; 
   } 
   BOOL inside = [self pointInside:point withEvent:event]; 
   UIView *hitView = nil; 
   if (inside) { 
      NSEnumerator *enumerator = [self.subviews reverseObjectEnumerator]; 
      for (UIView *subview in enumerator) { 
          hitView = [subview hitTest:point withEvent:event]; 
          if (hitView) { 
              break; 
          } 
      } 
      if (!hitView) { 
        hitView = self; 
      } 
      return hitView; 
  }else{ 
      return nil; 
  }
}

事件分发处理

  • 当找到Hit-test View之后,事件分发正式开始,如果Hit-test View能处理就直接处理,如果不能处理,就交给 The Responder Chain或者
    GestureRecognizer处理。
  • 获取可处理事件对象后调用这些对象touches回调方法。
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
  • 如果是UIGestureRecognizer事件,事件分发又不一样,经过- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event后走的是UIGestureRecognizerDelegate回调。
  • 当没有响应者,事件到AppDelegate后依旧未能响应,系统会放弃此次事件。

实际运用

  • 为某个button扩大点击区域,即使不在button区域也能响应button事件。
    重载UIButton的-(BOOL)pointInside: withEvent:方法,让Point即使落在Button的Frame外围也返回YES。
//in custom button .m
//overide this method
- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event {
    return CGRectContainsPoint(HitTestingBounds(self.bounds, self.minimumHitTestWidth, self.minimumHitTestHeight), point);
}

CGRect HitTestingBounds(CGRect bounds, CGFloat minimumHitTestWidth, CGFloat minimumHitTestHeight) {
    CGRect hitTestingBounds = bounds;
    if (minimumHitTestWidth > bounds.size.width) {
        hitTestingBounds.size.width = minimumHitTestWidth;
        hitTestingBounds.origin.x -= (hitTestingBounds.size.width - bounds.size.width)/2;
    }
    if (minimumHitTestHeight > bounds.size.height) {
        hitTestingBounds.size.height = minimumHitTestHeight;
        hitTestingBounds.origin.y -= (hitTestingBounds.size.height - bounds.size.height)/2;
    }
    return hitTestingBounds;
}
  • 不规则形状点击事件处理。
    重写-(BOOL)pointInside: withEvent:touches回调处理

相关文章

  • iOS 响应链

    iOS开发 - 事件传递响应链iOS 响应者链,事件的传递事件传递之响应链Cocoa Touch事件处理流程--响...

  • 深入浅出iOS事件机制

    深入浅出iOS事件机制事件传递:响应链事件传递响应链

  • 二、事件传递链和响应者链

    iOS触摸事件详解iOS开发-事件传递响应链 响应者链 UIResponser包括了各种Touch message...

  • 响应链

    iOS事件响应链中Hit-Test View的应用从iOS的事件响应链看TableView为什么不响应touche...

  • iOS响应者链

    参考好文 iOS开发-事件传递响应链,用运行时分析 iOS事件传递:响应者链[译] http://www.jian...

  • 事件的响应链与传递链

    iOS事件链有两条:事件的响应链;Hit-Testing事件的传递链 响应链:由离用户最近的view向系统传递。i...

  • iOS 中事件的响应链和传递链

    iOS事件链有两条:事件的响应链;Hit-Testing事件的传递链 响应链:由离用户最近的view向系统传递。i...

  • iOS 中事件的响应链和传递链

    iOS事件链有两条:事件的响应链;Hit-Testing事件的传递链 响应链:由离用户最近的view向系统传递。i...

  • iOS中对于响应链的理解

    对于响应链的理解: 在IOS中,有响应者链对事件进行响应,所有的响应类都是UIResponder的子类,响应者链是...

  • iOS 中事件的响应链和传递链

    iOS 事件的主要由:响应连 和 传递链 构成。一般事件先通过传递链,传递下去。响应链,如果上层不能响应,那么一层...

网友评论

    本文标题:iOS事件响应链

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