iOS事件的传递、分发及响应过程总结

  1. 查找合适的View
    产生触摸事件 → UIApplication事件队列 → UIWindow的hitTest:withEvent:→ UIView的hitTest:withEvent: → 子View的hitTest:withEvent: → 子View的hitTest:withEvent:
  • 在对象执行hitTest:withEvent:的过程中,如果对象自己的pointInside: withEvent:方法返回NO,就返回nil,否则开始查找所有的子View,一旦没有子View或者子View全部返回nil,就会把自己作为最合适View返回,UIWindow拿到最合适的View
  • 在将最合适的View返回给UIWindow之前,Event对象只是一个空壳,仅有timestamp属性有值,UIwindow拿到最合适的View之后,Evnet对象的touches数组里才有具体的touch对象。
  1. 事件分发
    UIApplication sendEvent: → UIWindow sendEvent: → 最合适的view开始响应
  • sendEvent:方法在查找到最合适的View之后才开始调用,故我认为查找最合适View的过程不属于事件分发过程,前者属于准备阶段。
  1. 事件响应
    根据事件类型调用对应方法,以touchBegan为例:
    最合适的view touchesBegan: withEvent: → 所在ViewController touchesBegan: withEvent:→ parentView touchesBegan: withEvent: → ... → UIWindow touchesBegan: withEvent: → UIAplication touchesBegan: withEvent: → AplicationDelegate touchesBegan: withEvent: → 结束
  • 如果某个View或ViewController未调用super touchesBegan: withEvent:则响应结束

你可能感兴趣的:(iOS事件的传递、分发及响应过程总结)