简体   繁体   English

Swift-在KeyWindow的subView中无法识别点击手势

[英]Swift - tap gesture is not recognized in subView of KeyWindow

I made a view that used as black overlay by adding itself to keyWindow. 通过将自身添加到keyWindow中,我制作了一个用作黑色覆盖的视图。 It shows as intended but tap gesture is not triggered. 它按预期显示,但未触发轻击手势。

class Menu{
let backView = UIView()
let menuWidth:CGFloat = 150
let menuView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = .white
    return cv
}()

init(){
    if let window = UIApplication.shared.keyWindow{
        backView.frame = window.frame
        backView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissHandler)))
        backView.backgroundColor = UIColor(white: 0, alpha: 0.5)
        backView.alpha = 0

        menuView.frame = CGRect(x: -1*menuWidth, y: 0, width: menuWidth, height: window.frame.height)
    }
}

func show(){
    if let window = UIApplication.shared.keyWindow{
        window.addSubview(backView)
        //window.addSubview(menuView)

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
            self.backView.alpha = 1
            self.menuView.frame = CGRect(x: 0, y: 0, width: self.menuWidth, height: window.frame.height)
        }, completion: nil)
    }
}

@objc func dismissHandler(){
    if let window = UIApplication.shared.keyWindow{
        UIView.animate(withDuration: 0.5, animations: {
            self.backView.alpha = 0
            self.menuView.frame = CGRect(x: -1*self.menuWidth, y: 0, width: self.menuWidth, height: window.frame.height)
        }){(isSuccess) in
            if(isSuccess){
                self.backView.removeFromSuperview()
                self.menuView.removeFromSuperview()
            }
        }
    }
}

I checked frame size of window and backview but they are same. 我检查了窗口和后视的帧大小,但它们是相同的。 Any idea why it is not working? 知道为什么它不起作用吗?

Solved: Problem was that I was making its instance in local scope. 解决:问题是我在本地范围内创建了它的实例。 Thanks matt for providing me the answer. 感谢马特为我提供答案。

In your original question, you didn't show how you use the Menu class itself to make anything happen. 在最初的问题中,您没有显示如何使用Menu类本身来进行任何事情。 But that could be where the problem is. 但这可能是问题所在。

For example, suppose you create the Menu class instance but you don't maintain it somehow. 例如,假设您创建了Menu类实例,但是您并未对其进行任何维护 Then you might be able to create the overlay, but after that, the Menu instance is gone, so the tap gesture recognizer subsequently has no target to talk to. 然后,您可能可以创建覆盖图,但是此后,Menu实例消失了,因此,点击手势识别器随后没有target可与之对话。

In other words, you've got this tap gesture configuration code: 换句话说,您已获得以下点击手势配置代码:

backView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissHandler)))

...but when the user taps and the tap gesture recognizer tries to send its message, your self has gone out of existence. ...但是当用户轻击而轻击手势识别器尝试发送其消息时,您的self已不复存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM