小胖说事29-----iOS中Navigation中左滑pop页面的三种方法

来源:互联网 发布:sql中count 1 的用法 编辑:程序博客网 时间:2024/05/16 17:40


本文转载自:点击打开链接

第三中类型,自定义任意位置返回页面的方式,上边的类就是.m,大家可以贴过去使用,这个类是继承NavigationController的,用这个类初始化rootController就可以了,这里还有源码可下载,完整的类:http://download.csdn.net/detail/haogaoming123/8906671

1.系统自带pop方法">系统自带pop方法

如果我们没有对navigation中的back按钮进行自定义,我们可以直接使用系统自带的左滑pop方法。但是如果我们对back按钮,进行了自定义,我们就要对self.navigationController.interactivePopGestureRecognizer这个属性进行设置了。关键代码:

[objc] view plaincopy
  1. __weak typeof(self) weakSelf = self;  
  2.    self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;  

下面是实例代码:

(继承AbeViewController类,就可以使用系统自带的pop方法。)

[objc] view plaincopy
  1. @interface AbeViewController ()<uigesturerecognizerdelegate>  
  2.    @end  
  3.    
  4.    @implementation AbeViewController  
  5.    - (void)viewDidLoad {  
  6.     [super viewDidLoad];  
  7.    }  
  8.    
  9. - (void)viewDidAppear:(BOOL)animated{  
  10.     //**************方法一****************//  
  11.     //设置滑动回退  
  12.     __weak typeof(self) weakSelf = self;                  
[objc] view plaincopy
  1. self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;  
  2.     //判断是否为第一个view  
  3.     if (self.navigationController && [self.navigationController.viewControllers count] == 1) {  
  4.         self.navigationController.interactivePopGestureRecognizer.enabled = NO;  
  5.     }  
  6. }  
  7.    
  8. #pragma mark- UIGestureRecognizerDelegate  
  9. //**************方法一****************//  
  10. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{  
  11.     return YES;  
  12. }  
  13.    
  14. @end  

2.自定义边缘左滑手势方法

          下面是实例代码:

 就是实现了一个手势方法,触发这个手势方法时pop。(继承AbeViewController类,就可以使用自定义边缘左滑手势的pop方法。)

[objc] view plaincopy
  1. @interface AbeViewController ()<uigesturerecognizerdelegate>  
  2.    @end  
  3.    @implementation AbeViewController  
  4.    - (void)viewDidLoad {  
  5.     [super viewDidLoad];  
  6.    }  
  7.    - (void)viewDidAppear:(BOOL)animated{  
  8.     //*************方法二*****************//  
  9.     UIScreenEdgePanGestureRecognizer *edgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanGesture:)];  
  10.     edgePanGestureRecognizer.delegate = self;  
  11.     edgePanGestureRecognizer.edges = UIRectEdgeLeft;  
  12.     [self.view addGestureRecognizer:edgePanGestureRecognizer];  
  13.   }  
  14.  - (void)didReceiveMemoryWarning {  
  15.     [super didReceiveMemoryWarning];  
  16.   }  
  17.    
  18. #pragma mark- private method  
  19. //*************方法二*****************//  
  20. - (void)edgePanGesture:(UIScreenEdgePanGestureRecognizer*)edgePanGestureRecognizer{  
  21.     [self.navigationController popViewControllerAnimated:YES];  
  22. }  
  23. #pragma mark- UIGestureRecognizerDelegate  
  24. //**************方法二****************//  
  25. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{  
  26.     if (self.navigationController && [self.navigationController.viewControllers count] == 1) {  
  27.         return NO;  
  28.     }  
  29.     return YES;  
  30. }  
  31. @end  

3.自定义view任何位置左移pop

      其实就是建立了一个UIPanGestureRecognizer手势,然后该手势触发方法,panGestureRecognizer.state pan的状态。
并且设置self.interactivePopGestureRecognizer.enabled = NO; 原生左滑无效
      下面是实例代码:在view中,任何位置左移触发pop方法。

知识点:[panGestureRecognizer locationInView:XX] 获取pan手势的CGPoint。(继承ABENavViewController类,就可以使用自定义view左滑手势的pop方法; ABENavViewController为UINavigationController的子类。

[objc] view plaincopy
  1. //  
  2. //  NavigationViewController.m  
  3. //  BaseProject  
  4. //  
  5. //  Created by haogaoming on 15/7/13.  
  6. //  Copyright (c) 2015年 郝高明. All rights reserved.  
  7. //  
  8.   
  9. #import "NavigationViewController.h"  
  10.   
  11. @interface NavigationViewController ()  
  12.   
  13. @property (nonatomic,strongUIImageView *backview;  
  14. @property (nonatomic,strongNSMutableArray *backImgs;  
  15. @property (nonatomic,assign) CGPoint panBeginPoint;  
  16. @property (nonatomic,assign) CGPoint panEndPoint;  
  17.   
  18. @end  
  19.   
  20. @implementation NavigationViewController  
  21.   
  22. - (id)init {  
  23.     self = [super init];  
  24.     self.delegate = self;  
  25.       
  26.     return self;  
  27. }  
  28. - (id)initWithRootViewController:(UIViewController *)rootViewController {  
  29.     self = [super initWithRootViewController:rootViewController];  
  30.     self.delegate = self;  
  31.       
  32.     return self;  
  33. }  
  34.   
  35. -(void)loadView  
  36. {  
  37.     [super loadView];  
  38.     self.backImgs = [NSMutableArray array];  
  39. }  
  40.   
  41. - (void)viewDidLoad {  
  42.     [super viewDidLoad];  
  43.       
  44.     //原生方法无效  
  45.     self.interactivePopGestureRecognizer.enabled = NO;  
  46.       
  47.     //设置手势  
  48.     [self.view AddGestureRecognizer:UIPanGestureRecognizerStyle delegate:self Section:@selector(panGestureRecognizerAction:)];  
  49. }  
  50.   
  51. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation  
  52. {  
  53.     return (toInterfaceOrientation == UIInterfaceOrientationPortrait);  
  54. }  
  55.   
  56. - (BOOL)shouldAutorotate  
  57. {  
  58.     return NO;  
  59. }  
  60.   
  61. -(UIViewController *)popViewControllerAnimated:(BOOL)animated  
  62. {  
  63.     [_backImgs removeLastObject];  
  64.     return [super popViewControllerAnimated:animated];  
  65. }  
  66.   
  67. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {  
  68.     if (self.viewControllers.count==1) {  
  69.         viewController.hidesBottomBarWhenPushed = YES;  
  70.     }  
  71.     //截图  
  72.     UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.sizeYES0.0f);  
  73.     [[UIApplication sharedApplication].keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];  
  74.     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();  
  75.     UIGraphicsEndImageContext();  
  76.     [self.backImgs addObject:img];  
  77.       
  78.     sleep(0.5); //防止没有截频成功  
  79.     [super pushViewController:viewController animated:animated];  
  80. }  
  81.   
  82. #pragma make-method  
  83. -(void)panGestureRecognizerAction:(UIPanGestureRecognizer *)panGesture  
  84. {  
  85.     if (self.viewControllers.count == 1) {  
  86.         return;  
  87.     }  
  88.   
  89.     if (panGesture.state == UIGestureRecognizerStateBegan) {  
  90.         //滑动开始  
  91.         self.panBeginPoint = [panGesture locationInView:[UIApplication sharedApplication].keyWindow];  
  92.         //插入图片  
  93.         [self insertLastViewFromSuperView:self.view.superview];  
  94.     }else if (panGesture.state == UIGestureRecognizerStateEnded){  
  95.         //滑动结束  
  96.         self.panEndPoint = [panGesture locationInView:[UIApplication sharedApplication].keyWindow];  
  97.         if (self.view.left >= (self.view.width/2.0)-50) {  
  98.             [UIView animateWithDuration:0.3 animations:^{  
  99.                 [self moveNavigationViewWithLength:[UIScreen mainScreen].bounds.size.width];  
  100.             } completion:^(BOOL finished) {  
  101.                 [self removeLastViewFromSuperview];  
  102.                 [self moveNavigationViewWithLength:0];  
  103.                 [self popViewControllerAnimated:NO];  
  104.             }];  
  105.         }else{  
  106.             [UIView animateWithDuration:0.3 animations:^{  
  107.                 [self moveNavigationViewWithLength:0];  
  108.             }];  
  109.         }  
  110.     }else{  
  111.         CGPoint point = [panGesture locationInView:[UIApplication sharedApplication].keyWindow];  
  112.         //防止右滑  
  113.         if ((point.x-self.panBeginPoint.x)<0) {  
  114.             return;  
  115.         }  
  116.         [self moveNavigationViewWithLength:(point.x-self.panBeginPoint.x)];  
  117.     }  
  118. }  
  119. /** 
  120.  *  更改frame的位置 
  121.  * 
  122.  *  @param lenght self.view的left位置 
  123.  */  
  124. -(void)moveNavigationViewWithLength:(CGFloat)lenght  
  125. {  
  126.     //图片位置设置  
  127.     self.view.frame = CGRectMake(lenght, self.view.topself.view.widthself.view.height);  
  128.     //图片动态阴影  
  129.     _backview.alpha = (lenght/[UIScreen mainScreen].bounds.size.width)*2/3 + 0.5;  
  130. }  
  131. /** 
  132.  *  将背景图插入到当前view的下边,然后通过改变self.view的frame实现滑动 
  133.  * 
  134.  *  @param supView 画布 
  135.  */  
  136. -(void)insertLastViewFromSuperView:(UIView *)supView  
  137. {  
  138.     //插入上一级视图背景  
  139.     if (_backview == nil) {  
  140.         _backview = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];  
  141.     }   _backview.image = [_backImgs lastObject];  
[objc] view plaincopy
  1. [supView insertSubview:_backview belowSubview:self.view];}/** * 移除背景图 */-(void)removeLastViewFromSuperview{ [_backview removeFromSuperview]; _backview = nil;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end  
0 0
原创粉丝点击