右滑返回interactivePopGestureRecognizer<转>
右滑手势返回
//自定义baseVC中
- (void)viewDidLoad { [super viewDidLoad]; // self.view.backgroundColor = ViewBgColor; [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack]; self.automaticallyAdjustsScrollViewInsets=NO; if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { self.navigationController.interactivePopGestureRecognizer.enabled = YES; if ([[UIApplication sharedApplication].activityViewController isKindOfClass:NSClassFromString(@"YMWelComeViewController")]) { self.navigationController.interactivePopGestureRecognizer.enabled = NO; } self.navigationController.interactivePopGestureRecognizer.delegate = self; } [self initNavBar]; } - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer {
//设置跟vc不可右滑 if ([[UIApplication sharedApplication].activityViewController isKindOfClass:NSClassFromString(@"YMHomePageViewController")] || [[UIApplication sharedApplication].activityViewController isKindOfClass:NSClassFromString(@"YMMineViewController")] || [[UIApplication sharedApplication].activityViewController isKindOfClass:NSClassFromString(@"YMOrderViewController")] || [[UIApplication sharedApplication].activityViewController isKindOfClass:NSClassFromString(@"YMVipCardViewController")] || [[UIApplication sharedApplication].activityViewController isKindOfClass:NSClassFromString(@"YMWelComeViewController")] ) { return NO; } //冲突后--无效 if(self.navigationController.childViewControllers.count == 1){ // 表示用户在根控制器界面,就不需要触发滑动手势, return NO; } return YES; }
ios7开始 苹果增加了页面 右滑返回的效果;具体的是以UINavigationController为容器的ViewController间右滑切换页面。
代码里的设置是:
self.navigationController.interactivePopGestureRecognizer.enabled = YES;(default is YES)
可以看到苹果给navigationController添加了一个手势(具体为UIScreenEdgePanGestureRecognizer(边缘手势,同样是ios7以后才有的)),就是利用这个手势实现的 ios7的侧滑返回。
问题1:然而事情并非我们想的那么简单。
1.当我们用系统的UINavigationController,并且也是利用系统的navigateBar的时候,是完全没有问题的
2.但是当我们没有用系统的navigateBar或者自定义了返回按钮的时候,这个时候 右滑返回是失效的。
解决(问题1)办法:对于这种失效的情况,考虑到interactivePopGestureRecognizer也有delegate属性,替换默认的self.navigationController.interactivePopGestureRecognizer.delegate来配置右滑返回的表现也是可行的。
我们可以在主NavigationController中设置一下:
self.navigationController.interactivePopGestureRecognizer.delegate =(id)self
问题2:
但是出现很多问题,比如说在rootViewController的时候这个手势也可以响应,导致整个程序页面不响应;push了多层后,快速的触发两次手势,也会错乱
解决(问题2)办法:
@interface NavRootViewController : UINavigationController
@property(nonatomic,weak) UIViewController* currentShowVC;
@end
@implementation NavRootViewController
-(id)initWithRootViewController:(UIViewController *)rootViewController
{
NavRootViewController* nvc = [super initWithRootViewController:rootViewController];
self.interactivePopGestureRecognizer.delegate = self;
nvc.delegate = self;
return nvc;
}
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
}
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (navigationController.viewControllers.count == 1)
self.currentShowVC = Nil;
else
self.currentShowVC = viewController;
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if (gestureRecognizer == self.interactivePopGestureRecognizer) {
return (self.currentShowVC == self.topViewController); //the most important
}
return YES;
}
@end
问题三:
UIScrollView上手势失灵:经研究,发现是UIScrollView上已经添加了 panGestureRecognizer(滑动)手势
【解决方案】
1.
苹果以UIGestureRecognizerDelegate的形式,支持多个UIGestureRecognizer共存。其中的一个方法是:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
一句话总结就是此方法返回YES时,手势事件会一直往下传递,不论当前层次是否对该事件进行响应。
@implementation UIScrollView (AllowPanGestureEventPass)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]){
return YES;
}else {
return NO;
}
}
这种方式就是建一个UISCrollVIew的category,然后我用的时候是全局用的,不知道为什么在快速返回上一层的时候并快速点击会照成线程卡死。 还有就是两个手势共存,所以在返回上一层的过程中,UIScrollerView还会滚动
2.
事实上,对UIGestureRecognizer来说,它们对事件的接收顺序和对事件的响应是可以分开设置的,即存在接收链和响应链。接收链如上文所述,和UIView绑定,由UIView的层次决定接收顺序。
而响应链在apple君的定义下,逻辑出奇的简单,只有一个方法可以设置多个gestureRecognizer的响应关系:
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;
//从UINavigationController里得到这个边缘手势
@implementation UINavigationController (iOS7Support)
@dynamic screenEdgePanGestureRecognizer;
- (UIScreenEdgePanGestureRecognizer *)screenEdgePanGestureRecognizer{
UIScreenEdgePanGestureRecognizer *screenEdgePanGestureRecognizer = nil;
if (self.view.gestureRecognizers.count > 0){
for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers){
if ([recognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]){
screenEdgePanGestureRecognizer = (UIScreenEdgePanGestureRecognizer *)recognizer;
break;
}
}
}
return screenEdgePanGestureRecognizer;
}
@end
//当screenEdgePanGestureRecognizer生效时,UIScrollerView的panGestureRecognizer失效,这样就解决了冲突
UIScreenEdgePanGestureRecognizer *screenEdgePanGestureRecognizer = self.navigationController.screenEdgePanGestureRecognizer;
[_scrollerView.panGestureRecognizer requireGestureRecognizerToFail:screenEdgePanGestureRecognizer];
全屏的右滑返回
FDFullscreenPopGesture这个开源项目里用运行时很简单的实现了全屏的右滑返回。
有人说直接用下面这种kvo的方式就能实现了,这是系统原有的私有属性,增大那个响应区域就可以了。但我自己还没实测过,有兴趣的可以试试。
[self.interactivePopGestureRecognizer setValue:@([UIScreen mainScreen].bounds.size.width) forKeyPath:@"_recognizer._settings._edgeSettings._edgeRegionSize"];
touchesbegan未响应
touchesbegan跟UITapGestureRecognizer同时存在时,tap会有一个属性是cancelsTouchesInView,默认为YES,设置为NO就可以使touchesbegan响应了。
参考:一个Bug引发的对UIGestureRecognizer的思考