bibscy
bibscy

Reputation: 2708

How to disable front ViewController's view when RearViewController's view is active SWRevealViewController in Swift 3.0

How to disable user's interaction with front view controller - namely with class ViewController in my case, when the rear menu is shown. With the current code, when rear menu is shown, bookCleaningButton is not disabled.

import UIKit
class ViewController: UIViewController, SWRevealViewControllerDelegate {

  @IBOutlet weak var menuButton: UIBarButtonItem!
  @IBOutlet weak var bookCleaningButton: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()
      self.revealViewController().rearViewRevealWidth = self.view.frame.width - 80

 //revela the menu if it is not nil
    if self.revealViewController() != nil {
        self.revealViewController().delegate = self
         menuButton.target = self.revealViewController()
           menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
         self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()   
    }
  }


func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
    if revealController.frontViewPosition == FrontViewPosition.left {
        self.view.isUserInteractionEnabled = false
    }
    else {
        self.view.isUserInteractionEnabled = true
     }
  }  
}

enter image description here

Upvotes: 2

Views: 812

Answers (2)

Deepakraj Murugesan
Deepakraj Murugesan

Reputation: 1227

In your front view controller class. Write this code on the viewdidload() method.

override func viewDidLoad() {
        super.viewDidLoad()

        menuBtn.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)

        revealViewController().delegate = self

        // Once time - See documented SWRevealViewController.h
        revealViewController().tapGestureRecognizer()
        revealViewController().panGestureRecognizer()
}

and then use this delegate from the SWRevealviewcontroller in the frontviewcontroller

func revealController(_ revealController: SWRevealViewController!, willMoveTo position: FrontViewPosition) {
        if position == FrontViewPosition.right {
            revealController.frontViewController.view.isUserInteractionEnabled = false
        }
        else {
            revealController.frontViewController.view.isUserInteractionEnabled = true
        }
    }

this will work up to your expectation....

Upvotes: 0

Gurtej Singh
Gurtej Singh

Reputation: 3234

You should handle this in your Menu view controller rather. The reveal view controller has access to the frontViewController and that property can be used to set the userInteractionEnabled as false.

So, in your Menu View Controller write this this code in the viewWillAppear method:

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.revealViewController().view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    self.revealViewController().frontViewController.revealViewController().tapGestureRecognizer()
    self.revealViewController().frontViewController.view.isUserInteractionEnabled = false
}

And in the same Menu view Controller add the following code in the viewWillDisappear method:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.revealViewController().frontViewController.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    self.revealViewController().frontViewController.view.isUserInteractionEnabled = true
}

The above code also adds other gestures, but those can be optional. The main action happens at these two lines:

self.revealViewController().frontViewController.view.isUserInteractionEnabled = false

self.revealViewController().frontViewController.view.isUserInteractionEnabled = true

Hope this solves your issue. Cheers.

Upvotes: 4

Related Questions