SWRevealViewController add gesture to entire front view

Matrosov Alexander picture Matrosov Alexander · Jul 24, 2013 · Viewed 7.8k times · Source

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

Answer

mluisbrown picture mluisbrown · Jul 24, 2013

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.