SWRevealViewController adds a gesture for the entire front view

I am currently using the SWRevealViewController class in my project. The basic functionality allows me to change the front view by pressing the button on the navigation bar. But I want to add a gesture to the whole view.

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

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

But it only works for one user interface element. Therefore, I cannot add, for example, another user interface element to this gesture.

This code below shows how the panGestureRecognizer method was 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; } 
+4
source share
6 answers

To add a gesture recognizer to the whole view, simply add it to the whole view instead of a single button. I use SWRevealViewController , and my main view is a UITableView , to make the gesture recognizer work for 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 presentation you like. For most UIViewContoller this will be self.view .

Obviously, if any controls or presentation routines have their own gesture recognizers, they will take precedence over the top-level view, so panning will only work in areas that are not occupied by these areas.

+9
source

// UPD:
It seems that my previous solution is redundant, you can just call getter to get the default behavior. I.e.

 [revealViewController panGestureRecognizer]; 

// old solution
You can also add the SWRevealViewController panGestureRecognizer to your own view, for example, in a subclass of SWRevealViewController , which may have:

 - (void)viewDidLoad { [super viewDidLoad]; // adds gesture recognizer to the whole thing [self.view addGestureRecognizer:self.panGestureRecognizer]; } 
+2
source

If you use a navigation controller and try to add a gesture recognizer to self.view, this gesture will not respond when scrolling through the navigation panel. What I did was go into the SWReaveal classes and add a second gesture recognizer. So now I add one recognizer in self.view and one in the navigation bar. Works great. I can use the code if you need it, but it should not be too complicated.

+1
source

To add a Gesture to the entire front view using the SWRevealViewController using the storyboard, we must add

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

in the FirstViewController application. Add an action and goal to the navigation bar, for example

 _sidebarButton.target = self.revealViewController; _sidebarButton.action = @selector(revealToggle:); // Set the gesture [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer]; 

In SWRevealViewController.h add the following method, for example

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

In 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 directly use

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

+1
source

In swift

 self.revealViewController().frontViewController.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) 
+1
source

If you use the navigation controller for your front view, you can add a pan gesture to the view of the navigation controller, like this

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

Thus, you can also scroll the navigation bar without changing any classes of the SWReveal controller.

0
source

Source: https://habr.com/ru/post/1493206/


All Articles