Search code examples
iosswrevealviewcontrollersetbackground

SWRevealViewController : Make Starting Background View Grey with custom animations


I am fairly new to iOS. I tried to look this up but couldn't find a definite answer to the following.

I have a SWRevealViewController to handle side menu bar from my FirstPage.

When the SWReveal appears after clicking btn_Menu (or by swiping left) I want the FirstPage to become grey and unusable.

I see that in FirstPage we have to do the following:

[btn_Menu addTarget:self.revealViewController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];

I need to change the revealToggle method somehow, or write a new selector in FirstPage which darkens it first, and then calls revealToggle ? I am unsure what's the best and easiest way to get this done. Thanks a lot.


Solution

  • In your FirstPage viewDidLoad do this code.

    - (void)viewDidLoad
    {
    SWRevealViewController *  revealController=[[SWRevealViewController alloc]init];
        revealController = [self revealViewController];
        [self.view addGestureRecognizer:revealController.panGestureRecognizer];
           revealController.delegate=self;
        [revealController panGestureRecognizer];
    
        [revealController tapGestureRecognizer];
        [btnSideMenu addTarget:revealController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
    
    }
    

    After that add this delegate method of SWRevealViewController in to your FirstPage.

    - (void)revealController:(SWRevealViewController *)revealController didMoveToPosition:(FrontViewPosition)position
    {
        if (revealController.frontViewPosition == FrontViewPositionRight)
        {
            UIView *lockingView = [UIView new];
            lockingView.translatesAutoresizingMaskIntoConstraints = NO;
    
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:revealController action:@selector(revealToggle:)];
            [lockingView addGestureRecognizer:tap];
            [lockingView addGestureRecognizer:revealController.panGestureRecognizer];
            [lockingView setTag:1000];
            [revealController.frontViewController.view addSubview:lockingView];
    
    
            lockingView.backgroundColor=[UIColor grayColor];
    
            NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(lockingView);
    
            [revealController.frontViewController.view addConstraints:
             [NSLayoutConstraint constraintsWithVisualFormat:@"|[lockingView]|"
                                                     options:0
                                                     metrics:nil
                                                       views:viewsDictionary]];
            [revealController.frontViewController.view addConstraints:
             [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[lockingView]|"
                                                     options:0
                                                     metrics:nil
                                                       views:viewsDictionary]];
            [lockingView sizeToFit];
        }
        else
            [[revealController.frontViewController.view viewWithTag:1000] removeFromSuperview];
    }