美文网首页
IOS里的响应链

IOS里的响应链

作者: 武_IOS书 | 来源:发表于2018-09-03 23:46 被阅读0次

    nextResponder指针指向的规则是:
    1、UIView
    如果 view 是一个 view controller 的 root view,nextResponder 是这个 view controller.
    如果 view 不是 view controller 的 root view,nextResponder 则是这个 view 的 superview
    2、UIViewController
    如果 view controller 的 view 是 window 的 root view, view controller 的 nextResponder 是这个 window
    如果 view controller 是被其他 view controller presented调起来的,那么 view controller 的 nextResponder 就是发起调起的那个 view controller
    3、UIWindow
    window 的 nextResponder 是 UIApplication 对象.
    4、UIApplication
    UIApplication 对象的 nextResponder 是 app delegate, 但是 app delegate 必须是 UIResponder 对象,并且不能使 view ,view controller 或 UIApplication 对象他本身.


    UIKit使用基于视图的hit-testing来确定touch事件发生的位置。具体解释就是,UIKit将touch的位置和视图层级中的view的边界进行了比较,UIView的方法 hitTest:withEvent: 在视图层级中进行,寻找包含指定touch的最深子视图。这个视图成为touch事件的第一个响应者。

    这里要注意几个点:

    符合响应者的条件包括
    1、touch事件的位置在响应者区域内
    2、响应者 hidden 属性不为 YES
    3、响应者 透明度 不是 0
    4、响应者 userInteractionEnabled 不为 NO
    5、遍历 subview 时,是从上往下顺序遍历的,即 view.subviews 的 lastObject 到 firstObject 的顺序,找到合适的响应者view,即停止遍历.

    相关文章

      网友评论

          本文标题:IOS里的响应链

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