SWRevealViewController: Remove interaction on frontview when rearview is revealed

前端 未结 15 1161
迷失自我
迷失自我 2020-12-24 10:05

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 t

15条回答
  •  生来不讨喜
    2020-12-24 10:34

    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!

提交回复
热议问题