一個觸摸事件的傳遞和響應(yīng)過程
1.發(fā)生觸摸時間后,系統(tǒng)生成一個UIEvent對象放到UIApplication管理的事件隊列中
2.UIApplication將事件取出,從UIWindow開始,深度優(yōu)先遍歷所有View,調(diào)用每個View的hitTest:withEvent:。
3.hitTest:withEvent:中先判斷點擊點是否在自身坐標系內(nèi)衷佃、hidden == YES、userInteractionEnabled == NO蹄葱,alpha <= 0.01)氏义。如果是False,直接返回nil新蟆,
4.如果自身不能響應(yīng)觅赊,依次調(diào)用子View的hitTest:withEvent:。
5.最終得到響應(yīng)事件的View琼稻。
6.UIWindow將事件傳遞給目標View吮螺。目標View判斷自身是否消費事件(自身添加了手勢或?qū)崿F(xiàn)了touchesBegan、touchesEnd)方案帕翻。如果自身可以消耗鸠补,事件傳遞結(jié)束。如果自身無法消耗嘀掸,傳遞給父View(touchesBegan調(diào)用父UIResponder的touchesBegan)紫岩。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self nextResponder] touchesBegan:touches withEvent:event];
}
UIControl->UiView->UIResponder->NSObject
UIResponde實現(xiàn)了下面四個方法,但是方法內(nèi)知識將事件傳遞給下一個UIResponde睬塌。真正的邏輯在UIControl里面
open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
open func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
open func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?)