【iOS界面开发】iOS事件派发机制

app启动时,UIApplicationMain方法会被调用,以创建一个UIApplication单例对象,它负责处理和派发系统发送给app事件队列的事件到合适的接收者。

事件来源分为三种:

  • UIControl Actions: these are the actions that are registered using the action/target pattern
  • User events: Events from user such as touches, shakes, motion, etc…
  • System events: Such as low memory, rotation, etc…

在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件。我们称之为“响应者对象”。UIApplication、UIViewController、UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件。

事件处理原理

  1. 当用户点击屏幕时,会产生一个触摸事件,系统会将该事件加入到一个由UIApplication管理的事件队列中;

  2. UIApplication会从事件队列中取出最前面的事件进行分发以便处理,通常,先发送事件给应用程序的主窗口(UIWindow);

  3. 主窗口会调用hitTest:withEvent:方法在视图(UIView)层次结构中找到一个最合适的UIView来处理触摸事件。

(hitTest:withEvent:其实是UIView的一个方法,UIWindow继承自UIView,因此主窗口UIWindow也是属于视图的一种)

hitTest:withEvent:方法的大致处理流程:

  • 调用当前视图的pointInside:withEvent:方法判断触摸点是否在当前视图内:

  • 若pointInside:withEvent:方法返回NO,说明触摸点不在当前视图内,则当前视图的hitTest:withEvent:返回nil

  • 若pointInside:withEvent:方法返回YES,说明触摸点在当前视图内,则遍历当前视图的所有子视图(subviews),调用子视图的pointInside:withEvent:方法。

注意:hitTest:withEvent:方法忽略隐藏(hidden=YES)的视图,禁止用户操作(userInteractionEnabled=NO)的视图,以及alpha级别小于0.01(alpha<0.01)的视图。

  1. 在找到的那个视图里处理,处理完后根据需要,利用响应链nextResponder可将消息往下一个响应者传递。

【关键】:要理解的有三点:

  1. iOS判断哪个界面能接受消息是从View层级结构的父View向子View传递,即树状结构的根节点向叶子节点递归传递。

  2. hitTest和pointInside成对,且hitTest会调用pointInside。

  3. iOS的消息处理是,当消息被人处理后默认不再向父层传递。

UIControl action messages dispatching

所以关于事件的链有两条:事件的响应链;Hit-Testing 时事件的传递链。

Hit-Testing 链:由系统向离用户最近的view传递。
UIKit –> active app’s event queue –> window –> root view –>……–>lowest view

响应链:由离用户最近的view向系统传递。
initial view –> super view –> …..–> view controller –> window –> Application –> AppDelegate

Hit-Test View 与 Hit-Testing

当用户与 iPhone 的触摸屏产生互动时,硬件就会探测到物理接触并且通知操作系统。操作系统就会创建相应的事件,并将其传递给当前正在运行的应用程序的事件队列。然后这个事件会被事件循环传递给优先响应对象,即 Hit-Test View 。

Hit-Test View 就是事件被触发时和用户交互的对象,寻找 Hit-Test View 的过程就叫做 Hit-Testing。

响应者链(responder chain)

  • 响应者链通常是由 initial view 开始;

  • UIView 的 nextResponder 是它的 superview;如果 UIView 已经是其所在的 UIViewController 的 root view,那么 UIView 的 nextResponder 就是 UIViewController;

  • UIViewController 如果有 Super ViewController,那么它的 nextResponder 为其 Super ViewController 最表层的 View;如果没有,即 UIViewController 的 view 是 UIWindow 的 root view ,那么它的 nextResponder 就是 UIWindow;

  • UIWindow 的 nextResponder 为 UIApplication;

  • UIApplication 的 nextResponder 是 app delegate, 且当仅且 app delegate 是 UIResponder 的实例,而不是一个 view,view controller 或者 app 对象本身。

注:我们可以重写任何类的nextResponder属性,以此返回不同的下一个响应者。

sendAction

对于一个给定的事件,UIControl会调用sendAction:to:forEvent:来将行为消息转发到UIApplication对象,再由UIApplication对象调用其sendAction:to:fromSender:forEvent:方法来将消息分发到指定的target上,而如果我们没有指定target,则会将事件分发到响应链上第一个想处理消息的对象上。而如果子类想监控或修改这种行为的话,则可以重写这个方法。

// UIControl
- (void)sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event;


// UIApplication
- (BOOL)sendAction:(SEL)action 
                to:(id)target 
              from:(id)sender 
          forEvent:(UIEvent *)event;

action selector

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event

猜你喜欢

转载自blog.csdn.net/freeWayWalker/article/details/70044949