【发布时间】:2017-09-08 11:36:00
【问题描述】:
我有一个视图,它显示一个菜单,其中一个表具有正确的透明背景以覆盖视图的其余部分,我可以在点击右侧时隐藏菜单,但无法点击表行。
-帧全屏
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)
-手势
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
}
我可以将 GestureRecognizer 添加到第二个 CGRect 吗?
谢谢!
【问题讨论】:
-
您不能将手势添加到 CGRect。您只能将其添加到视图中。
-
您可以使用所需的 CGRect b/w menuVC.view 和 self.view 添加一个更透明的视图,并向该视图添加手势
-
@Pablo 您可以只使用第 3 方 SideMenu。例如:jonkykong/SideMenu
-
我对 iOS 有点陌生,我不想让它变得太难,但我会尝试 Abhishek 推荐。感谢这个第 3 方的侧边菜单,太酷了