Passing touches between stacked UIWindows?

后端 未结 3 1343
不知归路
不知归路 2021-01-01 10:32

I have two windows -- a front one, and a back one. The front one is used to overlay some stuff on top of the back one. I want to be able to capture touches on some parts of

3条回答
  •  星月不相逢
    2021-01-01 10:54

    I had a similar issue: the additional UIWindow should have a transparent view on it with some intractable subviews. All touches outside of those intractable views must be passed through.

    I ended up using modified version of Anna's code that uses view's tag instead of type checking. This way view subclass creation is not required:

    class PassTroughWindow: UIWindow {
        var passTroughTag: Int?
    
        override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    
            let hitView = super.hitTest(point, with: event)
    
            if let passTroughTag = passTroughTag {
                if passTroughTag == hitView?.tag {
                    return nil
                }
            }
            return hitView
        }
    }
    

    Assuming you have a window and root view controller created you you could use it like this:

    let window: PassTroughWindow 
    //Create or obtain window
    
    let passTroughTag = 42
    
    window.rootViewController.view.tag = passTroughTag
    window.passTroughTag = passTroughTag
    
    //Or with a view:
    let untouchableView: UIView // Create it
    untouchableView.tag = passTroughTag
    window.addSubview(untouchableView)
    

提交回复
热议问题