SWRevealViewController: Remove interaction on frontview when rearview is revealed

Mou某 picture Mou某 · Mar 24, 2014 · Viewed 13.2k times · Source

I need to disable user interaction on front view when rear view is revealed. Found some others asking the same thing but can't really understand where or how to implement the code that I've seen.

Ex: I found this code from link,

- (void)revealController:(SWRevealViewController *)revealController 
      willMoveToPosition:(FrontViewPosition)position {
    if(position == FrontViewPositionLeft) {
        self.view.userInteractionEnabled = YES;
    } else {
        self.view.userInteractionEnabled = NO;
    } 
}

- (void)revealController:(SWRevealViewController *)revealController 
       didMoveToPosition:(FrontViewPosition)position {
    if(position == FrontViewPositionLeft) {
        self.view.userInteractionEnabled = YES;
    } else {
        self.view.userInteractionEnabled = NO;
    } 
}

Also found few other links

I have this code, but not really sure about the correct place to insert this code. I've tried adding it in my front/rear views and also in the SWRevealViewController method with no success

Appreciate if someone can point me in the right direction.

Answer

aj_f picture aj_f · May 26, 2014

I've recently come up with a solution that I wanted to share (sorry if it's 2 months late).

To disable user interaction on the Front View while the Menu is open, I added the following codes on my MenuViewController:

on viewWillAppear:

[self.revealViewController.frontViewController.view setUserInteractionEnabled:NO];

and on viewWillDisappear:

[self.revealViewController.frontViewController.view setUserInteractionEnabled:YES];

This will disable all user interactions on the Front View Controller, which means that the slide / tap gestures to CLOSE the menu will also be DISABLED.

Now, I have created a ParentViewController and made all the view controllers (the menu items) a subclass of it.

on my viewDidLoad, I put the following codes:

SWRevealViewController *revealController = [self revealViewController];
[revealController panGestureRecognizer];
[revealController tapGestureRecognizer];

If you run your app at this point, it would appear that the Tap Gesture works (a tap on the Front View will close the Menu), but NOT the Pan Gesture. I'm not sure why this is so, but in order to enable the slide gesture to CLOSE your menu, add the following code in your MenuViewController:

on viewWillAppear:

[self.revealViewController.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

To summarize, here's what you need:

On your MenuViewController:

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.revealViewController.frontViewController.view setUserInteractionEnabled:NO];
    [self.revealViewController.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    [self.revealViewController.frontViewController.view setUserInteractionEnabled:YES];
}

And on your menu items' view controller (you can make a ParentViewController for all of them):

-(void)viewDidLoad {
    [super viewDidLoad];

    SWRevealViewController *revealController = [self revealViewController];
    [revealController panGestureRecognizer];
    [revealController tapGestureRecognizer];
}

Hope this helps!