要使用故事板使用 SWRevealViewController 在整个前视图上添加手势,我们必须添加
SWRevealViewController *revealController = [self revealViewController];
[revealController panGestureRecognizer];
[revealController tapGestureRecognizer];
在应用程序的 FirstViewController 中。将操作和目标添加到导航栏按钮,例如
_sidebarButton.target = self.revealViewController;
_sidebarButton.action = @selector(revealToggle:);
// Set the gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
在 SWRevealViewController.h 添加以下方法,如
- (UITapGestureRecognizer*)tapGestureRecognizer;
- (BOOL)revealControllerTapGestureShouldBegin:(SWRevealViewController *)revealController;
在 SWRevealViewController.m 添加
- (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;
}
在 xib 的情况下,您可以直接使用
https://github.com/John-Lluch/SWRevealViewController