SWRevealViewController close rear view when tapping front view

前端 未结 9 1595
夕颜
夕颜 2020-12-09 04:18

I am using SWRevealViewController in order to implement a side nav menu in my app. I would like to make it so that the front view cannot be interacted with when

9条回答
  •  北海茫月
    2020-12-09 05:11

    Thanks to mixth & avdyushin for there mighty help. Here's the Swift 4 version derived from there answer to save your couple of hours:

    extension UIViewController: SWRevealViewControllerDelegate {
    
    func setupMenuGestureRecognizer() {
    
        revealViewController().delegate = self
    
        view.addGestureRecognizer(revealViewController().panGestureRecognizer())
        view.addGestureRecognizer(revealViewController().tapGestureRecognizer())
    }
    
    //MARK: - SWRevealViewControllerDelegate
    public func revealController(_ revealController: SWRevealViewController!, willMoveTo position: FrontViewPosition) {
        let tagId = 112151
        print("revealController delegate called")
        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.75, animations: {
                lock.alpha = 0.333
            }
            )
        }
    }
    
    }
    

    Now call this function setupMenuGestureRecognizer from viewDidLoad of your rear view controller.

    Or you can also implement SWRevealViewControllerDelegate directly to your rear view controller class and use the SWRevealViewControllerDelegate function in the class itself.

提交回复
热议问题