How to make SWRevealViewController menu disappear

2019-08-09 07:45发布

问题:

I have View controller with embedded SWRevealViewController I've added code below to disable any interaction while the menu on use.

The viewcontroller is embeded with tableview.

How to make menu disappears when users tap on the view-controller as the slack app "When the menu is on use and you tap on the chat the menu disappears"

  class Feed: UIViewController,SWRevealViewControllerDelegate {
@IBOutlet weak var menuButton:UIBarButtonItem!

@IBOutlet weak var menuButton:UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()

    if self.revealViewController() != nil {
        menuButton.target = self.revealViewController()
        menuButton.action = "revealToggle:"
        self.revealViewController().delegate = self
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

    }

    fetchMessages()
}





func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition)
{
    if position == FrontViewPosition.Left     // if it not statisfy try this --> if revealController.frontViewPosition == FrontViewPosition.Left
    {
        self.view.userInteractionEnabled = true
    }
    else
    {
        self.view.userInteractionEnabled = false
    }

    func revealController(revealController: SWRevealViewController!, didMoveToPosition position: FrontViewPosition)
    {
        if position == FrontViewPosition.Left     // if it not statisfy try this --> if revealController.frontViewPosition == FrontViewPosition.Left
        {
            self.view.userInteractionEnabled = true
        }
        else
        {
            self.view.userInteractionEnabled = false
        }

    }
}

回答1:

//set the delegate in your view controller class

class FeedVC: UIViewController,SWRevealViewControllerDelegate,UIGestureRecognizerDelegate 


 override func viewDidLoad() {
    super.viewDidLoad()

  self.revealViewController().delegate = self
    var swl=self.revealViewController()


    if swl != nil
    {
        swl.panGestureRecognizer()
        swl.tapGestureRecognizer()
    }
}

// create the left bar button action

@IBAction func but_back(sender: AnyObject) {

    self.navigationController?.interactivePopGestureRecognizer.delegate=self
    self.navigationItem.leftBarButtonItem?.target=self.revealViewController()
    self.navigationItem.leftBarButtonItem?.action=Selector("revealToggle:")
    self.revealViewController().revealToggle(sender)

    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}

//set the delegate method for SWLReval

func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition)
{
    if position == FrontViewPosition.Left     // if it not statisfy try this --> if revealController.frontViewPosition == FrontViewPosition.Left
    {
        self.view.userInteractionEnabled = true
        revealController.panGestureRecognizer().enabled=true
    }
    else
    {
        self.view.userInteractionEnabled = false
        revealController.panGestureRecognizer().enabled=false
    }



}

the updated code is available in this link , download the project in here