abhi1992 abhi1992 - 5 months ago 131
Objective-C Question

SWRevealViewController panGestureRecognizer with pageViewController iOS(side menu on swipe)


  1. How can I use panGestureRecognizer to open sideMenu if I already have some other swipe gestures in the frontViewController(in my case it is HomescreenViewController)?
    My frontViewController contains a pageViewController with 5 pages.So left swipe and right swipe are already enabled for the pageViewController.

    I used the following code in the viewDidLoad of frontViewController:

    _sideButton.target = self.revealViewController;
    _sideButton.action = @selector(revealToggle:);
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    [self.view addGestureRecognizer:self.revealViewController.tapGestureRecognizer];


    This will not do the work along with the pageViewController right swipe.
    Then I use this code:

    self.revealViewController.draggableBorderWidth = self.view.frame.size.width / 2;


    But this is also not working.I am using the latest SWRevealViewController version 2.4.0

    This is the view hierarchy of my frontViewController.
    HomescreenViewController>pagerView>PageviewController>listingViewController

  2. Also,When the side menu appears ,the portion of the frontviewController is active and when I try to close the side menu by swiping towards left, it recognises it as the pageviewcontroller swipe. I hope the solution to the first problem will solve this as well.



UPDATE(answer):
Look accepted answer for question number 2.
For first question,if you are experiencing a problem like mine,just create a view with width 20 pixels towards the left side using the code below and add panGestureRecognizer to this view(in
viewWillAppear
).

SWSwipeViewForPanGesture=[[UIView alloc]initWithFrame:CGRectMake(0, 0,20, self.view.frame.size.height)];
_SWSwipeViewForPanGesture.backgroundColor=[UIColor clearColor];
[self.view addSubview:_SWSwipeViewForPanGesture];
[_SWSwipeViewForPanGesture addGestureRecognizer:self.revealViewController.panGestureRecognizer];


Also,I had to add the following code in
- (void)revealController:(SWRevealViewController *)revealController didMoveToPosition:(FrontViewPosition)position
method This is a delegate method.So,you should confirm to the delegate in the viewdidload.

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


Thats it.It simply works!

Answer

Solution For First question:

You cannot recognize two swipe gesture on one view. So basically, answer for your first question is not possible. You cannot swipe for SWRevealViewController in your pageview controller.

Solution For Second question:

Write this code in HomescreenViewController ViewDidLoad method.

- (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 HomescreenViewController.

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

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

May be, it will help you.