如何在另一个 uiwindow 之外获取 uibutton 的触摸事件

How to get touch event for uibutton outside of another uiwindow

我在主 window 中有我的第一个 VC,当按下一个按钮时,我从右下角开始制作第二个 window 动画,它几乎停在整个屏幕的 3/4 处。一切都很好。问题是我在第二个 window 的外面有一个 cancelButton(红色 X),当我点击它时没有任何注册。我知道它在 parent 的范围之外,所以我尝试了 hitTest 但仍然没有。

func setAnchorsForCancelButton() {

    secondWindow?.addSubview(self.cancelButton)

    cancelButton.bottomAnchor.constraint(equalTo: secondWindow!.topAnchor, constant: -10).isActive = true

    cancelButton.leadingAnchor.constraint(equalTo: secondWindow!.leadingAnchor, constant: 10).isActive = true
    // width and height are 35
}

func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

    let translatedPoint = cancelButton.convert(point, from: secondWindow!.rootViewController!.view)

    if (cancelButton.bounds.contains(translatedPoint)) {
        return cancelButton.hitTest(translatedPoint, with: event)
    }
    return hitTest(point, with: event)
}

红色 X 是第二个 window 外侧的取消按钮。它没有接收到触摸事件

如何让它在第二个 window 之外接收触摸事件?

启动第二个 UIWindow 的代码:

class SecondWindow: NSObject {

    lazy var cancelButton: UIButton = {
        // button created
    }()

    var secondWindow: UIWindow?
    let webViewVC = WebViewController() // instagram gets shown in here
    let navVC: UINavigationController?

    override init() {
        super.init()
        // some Notifications are in here
    }

    func animateFromBottom() {

        guard let keyWindow = UIApplication.shared.keyWindow else { return }

        let startingFrame = CGRect(x: keyWindow.frame.width - 10,
                                   y: keyWindow.frame.height - 10,
                                   width: 10,
                                   height: 10)

        let endingRect = CGRect(x: 0,
                                y: 150,
                                width: keyWindow.frame.width,
                                height: keyWindow.frame.height)

         let navVC = UINavigationController(rootViewController: webViewVC)

         secondWindow = UIWindow(frame: startingFrame)
         secondWindow?.windowLevel = UIWindow.Level.normal
         secondWindow?.rootViewController = navVC!
         secondWindow?.makeKey()
         secondWindow?.isHidden = false

         // a function with an animation animates the second window to the endingFrame

         setAnchorsForCancelButton()
    }

    func setAnchorsForCancelButton() {

        secondWindow?.addSubview(self.cancelButton)

        cancelButton.bottomAnchor.constraint(equalTo: secondWindow!.topAnchor, constant: -10).isActive = true

        cancelButton.leadingAnchor.constraint(equalTo: secondWindow!.leadingAnchor, constant: 10).isActive = true

        // width and height are 35
    }

    func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

        let translatedPoint = cancelButton.convert(point, from: secondWindow!.rootViewController!.view)

        if (cancelButton.bounds.contains(translatedPoint)) {
            return cancelButton.hitTest(translatedPoint, with: event)
        }
        return hitTest(point, with: event)
    }
}

来自第一个 VC 的按钮启动第二个 window:

@obj func launchSecondWindow(_ sender: UIButton) {

    let secondWindow = SecondWindow()
    secondWidow.animateFromBottom()
}

我找到了答案here and here

我子class编辑了 UIWindow 并覆盖了其中的 hitTest。

class AnotherWindow: UIWindow {

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

        for subview in subviews.reversed() {

            let convertedPoint = subview.convert(point, from: self)

            if let candidate = subview.hitTest(convertedPoint, with: event) {

                return candidate
            }
        }

        return self
    }
}

然后在 SecondWindow 中 class 我使用了我的 subclass 代替:

// this was what I originally used
var secondWindow: UIWindow?

// **This is what I'm using now**
var secondWindow: AnotherWindow?