我正在使用SWReveal库来创建侧边菜单,我的目标是每当用户点击菜单栏时,它将打开侧面菜单并禁用前视图,并使前视图变为黑色并具有不透明度。 / p>
我需要按菜单汉堡栏返回前视图
我正试图模仿的例子
我的代码
import UIKit
class ViewController: UIViewController, SWRevealViewControllerDelegate {
@IBOutlet var menuBarBurger: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
revealViewController().delegate = self
setupSideMenu()
}
func setupSideMenu() {
if revealViewController() != nil {
menuBarBurger.target = self.revealViewController()
menuBarBurger.action = #selector(SWRevealViewController.revealToggle(_:))
revealViewController().rearViewRevealWidth = 300
view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
}
// This is the part where the background will turn blur and so on.
func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
let tagId = 8009
if revealController.frontViewPosition == FrontViewPosition.right {
let lock = self.view.viewWithTag(tagId)
UIView.animate(withDuration: 0.25, animations: {
lock?.alpha = 0
}, completion: {(finished: Bool) in
lock?.removeFromSuperview()
})
lock?.removeFromSuperview()
} else if revealController.frontViewPosition == FrontViewPosition.left {
let lock = UIView(frame: self.view.bounds)
lock.autoresizingMask = [.flexibleWidth, .flexibleHeight]
lock.tag = tagId
lock.alpha = 0
lock.backgroundColor = UIColor.black
lock.addGestureRecognizer(UITapGestureRecognizer(target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:))))
self.view.addSubview(lock)
UIView.animate(withDuration: 0.5, animations: {
lock.alpha = 0.333
})
}
}
}
故事板
tagid(以防万一你要问)
我应该怎样做才能达到相同的设计?
答案 0 :(得分:1)
您尚未将代理设置为self
revealViewController().delegate = self
假设revealViewController()
的类型为SWRevealViewController
,并且是您要显示的ViewController。