SWRevealViewController: Remove interaction on frontview when rearview is revealed

前端 未结 15 1240
迷失自我
迷失自我 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:35

    Although these solutions do work, I don't think any of them address the original question, which is actually quite simple:

    There are 2 steps involved:

    1) Add the following methods to your FrontViewController.m:

    - (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;
        } 
    }
    

    2) Make your front view controller be a delegate of SWRevealViewController in the FrontViewController.h file:

    (e.g. @interface HomeViewController : UIViewController <SWRevealViewControllerDelegate>)
    

    where my FrontViewController was named HomeViewController

    and also in the FrontViewController.m file with the following on ViewDidLoad:

    self.revealViewController.delegate = self;
    

    Problem solved! Much easier than creating parent classes, etc.

    0 讨论(0)
  • 2020-12-24 10:37

    Addition to hardluckbaby answer.

    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];
    

    It adds some undesired behavior, e.g. pan gesture will close rear view when starts on it.

    Default pan gesture may not work if you add it to your own view somewhere, something like on viewDidLoad of your front view controller:

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

    Remove such lines and this should works as expected, for pan and tap gestures

    SWRevealViewController *revealController = [self revealViewController];
    [revealController panGestureRecognizer];
    [revealController tapGestureRecognizer];
    
    0 讨论(0)
  • 2020-12-24 10:38
    On MenuTableViewController/ Rear VC, add SWRevealViewControllerDelegate.
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.revealViewController().delegate = self
    
            if self.revealViewController() != nil {
                self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
            }
        }
    
    Add this delegate method.
    
    func revealController(revealController: SWRevealViewController!, didMoveToPosition position: FrontViewPosition) {
            if(position.rawValue == 4)
            {
                //move to rear
                self.revealViewController().frontViewController.view.userInteractionEnabled =  false
            }
            else if (position.rawValue == 3)
            {
                //move to front - dashboard VC
                self.revealViewController().frontViewController.view.userInteractionEnabled =  true
            }
        }
        func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
    
    //will perform the same function as the above delegate method.
        }
    
    0 讨论(0)
  • 2020-12-24 10:42

    I was using the viewWillAppear and viewWillDisappear functions but as I have subviews for almost every item in the side menu I had. My issue was that I had a input field active (keyboard displaying) and accessed the side menu. In the root menu the keyboard hid but after I entered a submenu keyboard showed up again. To solve this I changed the approach to enable and disable the interaction in revealController like this:

    - (void)revealController:(SWRevealViewController *)revealController 
            didMoveToPosition:(FrontViewPosition)position {
        if (position == FrontViewPositionRight) {
            [self.revealViewController.frontViewController.view setUserInteractionEnabled:NO];
        } else if (position == FrontViewPositionLeft) {
            [self.revealViewController.frontViewController.view setUserInteractionEnabled:YES];
        }
    }
    
    0 讨论(0)
  • 2020-12-24 10:43

    I have used another approach to achieve the same outcome not sure if it helps.

    Assign SWRevealViewControllerDelegate to AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        SWRevealViewController* reveal = (SWRevealViewController*)self.window.rootViewController;
        reveal.delegate = self;
    
        // other bootstrapping code
    }
    

    and then in the delegate method -(void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position as below:

    -(void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position
    {
        if(position == FrontViewPositionLeft){
            [revealController.frontViewController.view setUserInteractionEnabled:YES];
            [revealController.frontViewController.revealViewController tapGestureRecognizer];
        }else{
            [revealController.frontViewController.view setUserInteractionEnabled:NO];
        }
    }
    

    UPDATED: added this line [revealController.frontViewController.revealViewController tapGestureRecognizer] to close the revealed controller when tap on frontviewcontroller

    0 讨论(0)
  • 2020-12-24 10:47

    As John Explained: Although these solutions do work, I don't think any of them address the original question, which is actually quite simple:

    There are 2 steps involved:

    1. Add the following methods to your FrontViewController.m:

      • (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; } }

    2. Make your front view controller be a delegate of SWRevealViewController in the FrontViewController.h file:

    (e.g. @interface HomeViewController : UIViewController ) where my FrontViewController was named HomeViewController

    and also in the FrontViewController.m file with the following on ViewDidLoad:

    self.revealViewController.delegate = self; Problem solved! Much easier than creating parent classes, etc.

    This will help you solve the user interactions for the FrontView controller lovely I would just add the following change taken from Xun's response also above and you will solve both the user interactions and the hide menu when user taps FrontViewController.

    - (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;
          //Hides the menu when user taps FrontViewController
           [revealController.frontViewController.revealViewController tapGestureRecognizer];
        } 
    }
    
    0 讨论(0)
提交回复
热议问题