iOS

iOS响应链和事件传递原理小结

2018-04-19  本文已影响199人  Hedgehog___

前言

  在日常的开发过程我们经常遇到子视图在父视图外面点击无响应的情况,我们通常用hitTest:withEvent:方法和pointInside方法,那么这两个方法究竟实现原理是怎样的呢,我们就来探究一下。

1、UIResponder(响应者对象)

  我们都不难发现我们经常用到的控件,类似UIButton、UILabel这些都是可以响应事件,而他们都继承于UIResponder。在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接受并处理事件,我们称之为“响应者对象”。

//触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
//加速计事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
//远程控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;

  其中加速计事件和远程控制事件我们暂不讲解。主要研究一下触摸事件的处理,这四个方法的调用时机(都是系统调用的):

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

2、事件的产生和传递

2.1、hitTest:withEvent:方法

  hitTest:withEvent:方法当事件传递到某个控件的时候,系统就会调用改控件的hitTest:withEvent:方法,返回合适的控件

  遍历子控件有就返回,没有就返回self,返回nil则表示需要父视图处理,其他子视图都不能处,hit:withEvent:方法底层会调用pointInside:withEvent:方法判断点在不在方法调用者的坐标系上,不在返回nil。

  我们可以通过重新控件的hitTest:withEvent:方法来改变处理事件的控件。想让谁处理就谁处理。

2.2、pointInside:withEvent:方法

  pointInside:withEvent:方法判断点在不在当前view上(方法调用者的坐标系上)如果返回YES,代表点在方法调用者的坐标系上;返回NO代表点不在方法调用者的坐标系上,那么方法调用者也就不能处理事件。

3、事件响应

  我们事件传递找到合适的视图view的时候首先看view能否处理这个事件,如果能处理则交由其处理并停止该事件的向上响应(各种事件、滑动、touches...),如果不能则会将事件传递给其上级视图(view的superView);如果上级视图仍然无法处理则会继续往上传递;一直到 window,如果window还是不能处理此事件则继续交给application处理,如果最后application还是不能处理此事件则将其丢弃。
  如果实现某个touches..并在其中调用[supertouches….];则父视图子视图可以同时响应。

总结

  iOS的事件传递是从下到上的(父视图-->子视图),有不能接收的(1、userInteractionEnabled = NO 2、隐藏 3、透明度<0.01)则停止向下传递。事件响应是从上到下的,有能响应的并没有进行相关处理的([supertouches….])则停止向上响应。
本文借鉴:https://www.jianshu.com/p/2e074db792ba

上一篇 下一篇

猜你喜欢

热点阅读