iosswiftswrevealviewcontroller

SWRevealViewController - How can I make the menu disappear with a tap and/or swipe on FrontViewController


I am developing a locator map. How can I make the menu disappear with a tap and/or swipe on FrontViewController. My front view controller displays a Map (Google Map).


Solution

  • You can enable the use of swipe gestures to close your menu in the SWRevealViewController by adding its pan gesture recognizer to your view in viewDidLoad: of your view controller.

    override func viewDidLoad() 
    {
        super.viewDidLoad()                 
        view.addGestureRecognizer(myRevealViewController.panGestureRecognizer())
    }
    

    Getting the menu to respond to taps requires handling the tap action separately with a tap gesture recognizer.

    For example, with a tap gesture recognizer defined and added to your view like

    myTapGestureRecognizer = UITapGestureRecognizer(target: self, action: "closeMenu")
    view.addGestureRecognizer(myTapGestureRecognizer)
    

    You could close the menu with a function using:

    func closeMenu()
    {      
        myRevealViewController?.setFrontViewPosition(FrontViewPosition.Left, animated: true)
    }
    

    where the .Left for FrontViewPosition is dependent on how you have your view controllers configured for your SWRevealViewController.