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

苹果一直都在人机交互中尽力做到极致,在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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值