美文网首页iOS开发技术文章
iOS 事件传递和事件链

iOS 事件传递和事件链

作者: 小怪兽大作战 | 来源:发表于2021-11-03 09:11 被阅读0次

    一个触摸事件的传递和响应过程

    1.发生触摸时间后,系统生成一个UIEvent对象放到UIApplication管理的事件队列中
    2.UIApplication将事件取出,从UIWindow开始,深度优先遍历所有View,调用每个View的hitTest:withEvent:。
    3.hitTest:withEvent:中先判断点击点是否在自身坐标系内、hidden == YES、userInteractionEnabled == NO,alpha <= 0.01)。如果是False,直接返回nil,
    4.如果自身不能响应,依次调用子View的hitTest:withEvent:。
    5.最终得到响应事件的View。
    6.UIWindow将事件传递给目标View。目标View判断自身是否消费事件(自身添加了手势或实现了touchesBegan、touchesEnd)方案。如果自身可以消耗,事件传递结束。如果自身无法消耗,传递给父View(touchesBegan调用父UIResponder的touchesBegan)。

     - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [[self nextResponder] touchesBegan:touches withEvent:event];
    }
    
    

    UIControl->UiView->UIResponder->NSObject

    UIResponde实现了下面四个方法,但是方法内知识将事件传递给下一个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?)
    

    https://www.jianshu.com/p/df86508e2811

    相关文章

      网友评论

        本文标题:iOS 事件传递和事件链

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