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

时间:2019-06-22 21:04:23

标签: ios swift uibutton hittest

我在主窗口中有我的第一个VC,当按下按钮时,我从右下角开始动画第二个窗口,该窗口几乎停在整个屏幕的3/4处。一切动画效果都很好。问题是我在第二个窗口的外面有一个cancelButton(红色X),当我点击它时,没有任何寄存器。我知道它不在父母的范围之内,所以我尝试了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是第二个窗口外部的取消按钮。它没有收到触摸事件

如何在仍处于第二个窗口之外的状态下获取触摸事件?

enter image description here

启动第二个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)
    }
}
firstVC中的

按钮可启动第二个窗口:

@obj func launchSecondWindow(_ sender: UIButton) {

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

1 个答案:

答案 0 :(得分:0)

我找到了答案herehere

我继承了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类内部,我改用子类:

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

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