4

Right now I am using SWRevealViewController class in my project. The basic functionality allows me to swap front view by pressing navigation bar button. But I want to add gesture to entire view.

I can add this code and it works for my button.

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

But it just works for the one UI element. So I can't add, for example, other UI element to this gesture.

This code below shows how panGestureRecognizer method has been written:

- (UIPanGestureRecognizer*)panGestureRecognizer
{
    if ( _panGestureRecognizer == nil )
    {
        SWDirectionPanGestureRecognizer *customRecognizer =
            [[SWDirectionPanGestureRecognizer alloc] initWithTarget:self action:@selector(_handleRevealGesture:)];

        customRecognizer.direction = SWDirectionPanGestureRecognizerHorizontal;
        customRecognizer.delegate = self;
        _panGestureRecognizer = customRecognizer ;
    }
    return _panGestureRecognizer;
}
Mark Webb
  • 39
  • 10
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • Is it compulsory that in order any view controller to implement the swipe to reveal activity must be inter connected in some way with the RevealViewController in the storyboard. I am having a problem with that? – G.Abhisek Feb 17 '16 at 04:43

6 Answers6

9

To add the gesture recognizer to the whole view just add it to the whole view instead of just a single button. I'm using SWRevealViewController and my main view is a UITableView so to get the gesture recognizer to work on the whole view I have this in the viewDidLoad method of my UIViewController:

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

So, just add the recognizer to the view you like. For most UIViewContollers this will be self.view.

Obviously if any controls or subviews of the view have their own gesture recognisers, these will take precedence of the one on the top level view, such that the panning will only work in the areas not occupied by those subviews.

mluisbrown
  • 14,448
  • 7
  • 58
  • 86
  • 1
    yes it is work in viewDidLoad. I have missed one thing. [self.navigationController.navigationBar addGestureRecognizer:revealController.panGestureRecognizer] line goes after self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer]line and there was an overwrite. Can I also add gesture to navigation bar button and to the view as well? – Matrosov Oleksandr Jul 24 '13 at 14:24
  • can I also add touch up inside gesture to my view? or there is already made method in code. I mean when front view toggled to the left. – Matrosov Oleksandr Jul 24 '13 at 14:33
  • 1
    @MatrosovAlexander why would you want the gesture recognizer on the nav bar button as well? If you add it to the nav bar then the whole nav bar gets the gesture recognizer. Normally you want the button just to toggle the front view to the left. You can have an action method for your button which does: `[self.revealViewController revealToggle:sender];` which will toggle the front view to the side and then back again. – mluisbrown Jul 24 '13 at 14:45
  • yes I know about it, but can I add gesture as well or not? thanks – Matrosov Oleksandr Jul 24 '13 at 15:37
  • 1
    @MatrosovAlexander I don't see any reason you couldn't add a gesture recognizer as well, all though I don't know why you would want to. – mluisbrown Jul 24 '13 at 15:52
  • My main view is of UIViewController and i am adding it like, [self.view addGestureRecognizer: self.revealViewController.panGestureRecognizer]; but it doesn't work. – Programming Learner Nov 19 '14 at 10:57
  • @mluisbrown Is it compulsory that in order any view controller to implement the swipe to reveal activity must be inter connected in some way with the RevealViewController in the storyboard. I am having a problem with that in which I have a ViewController not connected with the RevealViewController in the storyboard and still I need to implement the swipe & other functionalities. – G.Abhisek Feb 17 '16 at 04:45
2

//UPD:
Seems my previous solution is a overkill, you just could call getter to get the default behaviour. I.e.

[revealViewController panGestureRecognizer];

// old solution
You could also add SWRevealViewController's panGestureRecognizer to it's own view, for example in a subclass of SWRevealViewController you may have:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // adds gesture recognizer to the whole thing
    [self.view addGestureRecognizer:self.panGestureRecognizer];
}
MANIAK_dobrii
  • 6,014
  • 3
  • 28
  • 55
1

If you're using a navigation controller and try to add the gesture recognizer to self.view, the gesture won't respond when swiping on the navigation bar. What I did was go into SWReaveal Classes and add a second gesture recognizer. So now I add one recognizer to self.view and one to the navigation bar. Works perfectly. I can share code if you need it but it shouldn't be too hard

MobileMon
  • 8,341
  • 5
  • 56
  • 75
  • while this works, I think a better solution is to add the original gesture recognizer to self.navigationController.view instead of self.view – MobileMon Feb 19 '14 at 17:07
1

To add Gesture on entire front view using SWRevealViewController using storyboard, we have to add

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

in the the FirstViewController of the app. Add Action and Target to the Navigation bar button like

_sidebarButton.target = self.revealViewController;
_sidebarButton.action = @selector(revealToggle:);

// Set the gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

In the SWRevealViewController.h add following method like

- (UITapGestureRecognizer*)tapGestureRecognizer;
- (BOOL)revealControllerTapGestureShouldBegin:(SWRevealViewController *)revealController;

In the SWRevealViewController.m add

- (UITapGestureRecognizer*)tapGestureRecognizer
{
if ( _tapGestureRecognizer == nil )
{
    UITapGestureRecognizer *tapRecognizer =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_handleTapGesture:)];

    tapRecognizer.delegate = self;
    [_contentView.frontView addGestureRecognizer:tapRecognizer];
    _tapGestureRecognizer = tapRecognizer ;
}
return _tapGestureRecognizer;

}

- (void)_handleTapGesture:(UITapGestureRecognizer *)recognizer

{

 NSTimeInterval duration = _toggleAnimationDuration;
[self _setFrontViewPosition:FrontViewPositionLeft withDuration:duration];

}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ( _animationQueue.count == 0 )
{
    if ( gestureRecognizer == _panGestureRecognizer )
       // return [self _panGestureShouldBegin];
        return ( gestureRecognizer == _panGestureRecognizer && _animationQueue.count == 0) ;

    if ( gestureRecognizer == _tapGestureRecognizer )
        return [self _tapGestureShouldBegin];
}

return NO;

}

In case of xib you can direct use

https://github.com/John-Lluch/SWRevealViewController

Programming Learner
  • 4,351
  • 3
  • 22
  • 34
1

On Swift

self.revealViewController().frontViewController.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
0

If you are using a navigation controller for your front view you can add the pan gesture to the nav controller's view like this

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

That way you can also swipe on the navigation bar without altering any classes of the SWReveal controller.

Andrespch
  • 370
  • 3
  • 16