revealViewController委托不起作用

时间:2017-10-26 07:55:55

标签: ios swift swrevealviewcontroller

我正在使用SWReveal库来创建侧边菜单,我的目标是每当用户点击菜单栏时,它将打开侧面菜单并禁用前视图,并使前视图变为黑色并具有不透明度。 / p>

我需要按菜单汉堡栏返回前视图

我正试图模仿的例子

enter image description here

我的代码

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
        })
    }
}
}

故事板

enter image description here

tagid(以防万一你要问)

enter image description here

我应该怎样做才能达到相同的设计?

1 个答案:

答案 0 :(得分:1)

您尚未将代理设置为self

revealViewController().delegate = self

假设revealViewController()的类型为SWRevealViewController,并且是您要显示的ViewController。