UIGestureRecognizer仅限于CGRect

时间:2017-09-08 11:36:00

标签: ios swift

我有一个视图,显示一个菜单,其中一个表格具有正确的透明背景,以覆盖视图的其余部分,我可以在点击右侧时隐藏菜单,但是表格行不能被点击。< / p>

-frame full screen

self.menuVC.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
self.menuVC.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)

-Gesture

        self.tapBackground = UITapGestureRecognizer(target: self, action: #selector(self.hideTapMenu))
        self.asda = CGRect(x: self.table.bounds.width, y: 0.0, width: self.view.frame.width, height: self.view.frame.height)
        self.view.addGestureRecognizer(self.tapBackground)


        self.addChildViewController(self.menuVC)
        self.view.addSubview(self.menuVC.view)

隐藏菜单

 func hideTapMenu (gesture: UIGestureRecognizer) {
    let p = gesture.location(in: self.view)

    if asda.contains(p){
        hideMenu()
    } else {
        print("Touching menu")
    }

}


func hideMenu(){

    UIView.animate(withDuration: 0.4, animations: { () -> Void in

        self.menuVC.view.frame = CGRect(x: -UIScreen.main.bounds.size.width, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
    }) {(finished) in

        self.menuVC.view.removeFromSuperview()
    }

    AppDelegate.isMenuShown = true
}

this is the screen

我可以将GestureRecognizer添加到第二个CGRect吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以使用UIGestureRecognizerDelegate来处理它。在代码中实现其gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)方法以返回true - 这样您的手势识别器将起作用,但它也将允许识别其他手势。这样表格的内部手势识别器应能正常工作。

代码:

// add this to initializing code to set its delegate to self
self.tapBackground.delegate = self

代表实施:

extension YourViewController: UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

答案 1 :(得分:0)

您可以使用其他视图(如图像视图)并将该手势添加到图像视图,而不是使用主视图控制器来设置模糊效果。 表视图应该是最顶层的视图。