【问题标题】:How to get touch event for uibutton outside of another uiwindow如何在另一个uiwindow之外获取uibutton的触摸事件
【发布时间】:2019-06-22 21:04:23
【问题描述】:

我的第一个VC 在主窗口中,当按下按钮时,我从右下角为第二个窗口设置动画,该窗口几乎停在整个屏幕的 3/4 处。一切都很好。问题是我在第二个窗口外面有一个取消按钮(红色 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 是位于第二个窗口外部的取消按钮。它没有接收到触摸事件

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

启动第二个 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()
}

【问题讨论】:

  • 您是否考虑过简单地扩展包含取消按钮的视图?即,将取消按钮限制在视图内的左上角 - 然后将 Instagram 视图限制在其下方。然后将backgroundColor 设置为clear
  • @jake 我找到了答案,谢谢你的建议:)

标签: ios swift uibutton hittest


【解决方案1】:

我找到了答案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?

【讨论】:

  • 非常适合我。 .convert 真的好用!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-26
相关资源
最近更新 更多