Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27133

SWRevealViewController add gesture to entire front view

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

Upvotes: 4

Views: 7827

Answers (6)

Samuel Joseph
Samuel Joseph

Reputation: 11

On Swift

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

Upvotes: 1

Programming Learner
Programming Learner

Reputation: 4411

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

Upvotes: 1

Andrespch
Andrespch

Reputation: 367

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.

Upvotes: 0

MANIAK_dobrii
MANIAK_dobrii

Reputation: 6032

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

Upvotes: 2

MobileMon
MobileMon

Reputation: 8651

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

Upvotes: 1

mluisbrown
mluisbrown

Reputation: 14918

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.

Upvotes: 9

Related Questions