ios侧滑返回:完美解决 interactivePopGestureRecognizer 卡住的

来源:互联网 发布:qq群群发软件 编辑:程序博客网 时间:2024/04/28 20:15

苹果一直都在人机交互中尽力做到极致,在iOS7中,新增加了一个小小的功能,也就是这个api:self.navigationController.interactivePopGestureRecognizer.enabled = YES;

这个api功能就是在NavigationController堆栈内的UIViewController可以支持右滑手势,也就是不用点击右上角的返回按钮,轻轻在屏幕左边一

滑,屏幕就会返回,随着ios设备屏幕的增大,这个小功能让手指短,拇指大和手残人士看到了福音。

这个功能是好,但是经常我们会有需求定制返回按钮,如果手动定制了返回按钮,这个功能将会失效,也就是自定义了navigationItem的leftBarButtonItem,那么这个手势就会失效。解决方法找到两种 

 1.重新设置手势的delegate

 self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

 2.当然你也可以自己响应这个手势的事件

 [self.navigationController.interactivePopGestureRecognizer addTarget:self action:@selector(handleGesture:)];

有更多方法以后继续补充,这里可以根据自己需要进行选择,如果只是简单定制了返回按钮,第一种最简单,一句代码搞定。


interactivePopGestureRecognizer是iOS7推出的解决VeiwController滑动后退的新功能,虽然很实用,但是坑也很多啊(比如在rootViewcontroller下,使用侧滑返回手势,可能就卡住了),这里给出如何完美解决interactivePopGestureRecognizer卡住的问题.

当然我们要自定义UINavigationController来解决这个问题:

#import "MMNavController.h"@interface MMNavController (){    }@end@implementation MMNavController- (id)initWithRootViewController:(UIViewController *)rootViewController{    self = [super initWithRootViewController:rootViewController];    if (self) {        // Custom initialization            }    return self;}- (void)viewDidLoad{        __weak MMNavController *weakSelf = self;        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])    {        self.interactivePopGestureRecognizer.delegate = weakSelf;                self.delegate = weakSelf;    }    }- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{        if ( [self respondsToSelector:@selector(interactivePopGestureRecognizer)]  animated == YES )    {        self.interactivePopGestureRecognizer.enabled = NO;    }        [super pushViewController:viewController animated:animated];    }- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated{    if ( [self respondsToSelector:@selector(interactivePopGestureRecognizer)]  animated == YES )    {        self.interactivePopGestureRecognizer.enabled = NO;    }        return  [super popToRootViewControllerAnimated:animated];    }- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated { 
if( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] ) { 
self.interactivePopGestureRecognizer.enabled = NO; 
return [super popToViewController:viewController animated:animated]; } 
#pragma mark UINavigationControllerDelegate 
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController
 animated:(BOOL)animate { 
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) 
{ self.interactivePopGestureRecognizer.enabled = YES; } 
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { 
if ( gestureRecognizer ==self.interactivePopGestureRecognizer ) { 
if ( self.viewControllers.count 2 || self.visibleViewController == [self.viewControllers objectAtIndex:0] )
return NO; } 
return YES; 
@end
        

0 0