说一说响应链

2018-06-09  本文已影响5人  枫韵海

响应者对象

在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接受并处理事件,我们称之为“响应者对象”。

UIApplication
UIViewController
UIView

继承自UIResponder的类能够处理事件是因为提供了以下方法

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?)

响应者对象和响应者链

事件的产生、传递和响应

事件的产生

事件的传递

当一个事件发生后,事件会从父控件传给子控件,也就是说由UIApplication -> UIWindow -> UIView -> 子类view,以上就是事件的传递,也就是寻找最合适的view的过程。

事件的响应

也就是说:事件的传递是从上到下(父控件到子控件),事件的响应是从下到上(顺着响应者链条向上传递:子控件到父控件。

事件的拦截

这里就涉及到 hitTest 方法了

举例说明

FAE21D58-CF5E-4A8B-BE58-4553600569FF.png

层级结构是这样的F->B->A,如果我想点击A,B,F都只让B响应,我们可以这样做:

class UIViewB: UIView {
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("UIViewB")
        super.touchesBegan(touches, with: event)
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        print("UIViewB - hitTest")
        return self
    }
    
}

这样不管点击A还是B或者F,B都会处理,同时我们也会发现即便在F里面有

class UIViewF: UIView {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("UIViewF")
        super.touchesBegan(touches, with: event)
    }

}

也并不会打印"UIViewF", 说明UIViewF的此方法被拦截了。但是如果UIViewA里面有此方法确会被调用。

让我们在UIViewF里面和UIViewB一样加上hitTest:withEvent方法

class UIViewF: UIView {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("UIViewF")
        super.touchesBegan(touches, with: event)
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        print("UIViewF - hitTest")
        return self
    }

}

打印结果如下:

UIViewB - hitTest
UIViewB - hitTest
UIViewB
UIViewA

UIViewF的拦截方法没有起作用,这说明hitTest方法是由父视图开始->子视图

那么由此推断,如果我们在父视图UIViewA

class UIViewA: UIView {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("UIViewA")
        super.touchesBegan(touches, with: event)
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        print("UIViewA - hitTest")
        return nil
    }
    
}

这样应该不会打印A,B,F的touchesBegan方法里面的东西,经过测试确实如此。

问题

上图可知层级顺序,如果我点击超出父视图UIViewB部分的UIViewF会怎样

打印结果如下

UIViewA

如果看来超出父视图范围不会响应自己的touchesBegan方法

那么问题来了,如果我想要超出父视图办法仍然响应自己呢

class UIViewA: UIView {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("UIViewA")
        super.touchesBegan(touches, with: event)
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        print("UIViewA - hitTest")
        let view = super.hitTest(point, with: event)
        let viewF = viewWithTag(10)
        if let viewF = viewF {
            if viewF.frame.contains(point) {
                return viewF
            }
        }
        return view
    }
    
}

我给UIViewF设置了个tag,方便找到它

打印结果如下

UIViewA - hitTest
UIViewA - hitTest
UIViewF
UIViewB
UIViewA

疑问

上一篇下一篇

猜你喜欢

热点阅读