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

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

假设我们没有对navigation中的backbutton进行自己定义,我们能够直接使用系统自带的左滑pop方法。

可是假设我们对backbutton。进行了自己定义,我们就要对self.navigationController.interactivePopGestureRecognizer这个属性进行设置了。关键代码:

  1. __weak typeof(self) weakSelf = self;
  2. self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;

以下是实例代码:

(继承AbeViewController类。就能够使用系统自带的pop方法。)

  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;  
  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方法。

  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的子类。

  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,strong) UIImageView *backview;
  14. @property (nonatomic,strong) NSMutableArray *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.size, YES, 0.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.top, self.view.width, self.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];
  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
  1.  

小胖说事29-----iOS中Navigation中左滑pop页面的三种方法的更多相关文章

  1. VC中加载LIB库文件的三种方法

    VC中加载LIB库文件的三种方法 在VC中加载LIB文件的三种方法如下: 方法1:LIB文件直接加入到工程文件列表中   在VC中打开File View一页,选中工程名,单击鼠标右键,然后选中&quo ...

  2. Linux 中执行Shell 脚本的方式(三种方法)

    Shell 脚本的执行方式通常有如下三种: (1)bash script-name 或者 sh script-name:(2)path/script-name或者./script-name:(3)so ...

  3. Java中实现十进制数转换为二进制的三种方法

    第一种:除基倒取余法 这是最符合我们平时的数学逻辑思维的,即输入一个十进制数n,每次用n除以2,把余数记下来,再用商去除以2...依次循环,直到商为0结束,把余数倒着依次排列,就构成了转换后的二进制数 ...

  4. C#中Math类的计算整数的三种方法

    1.Math.Round:四舍六入五取偶 引用内容 Math.Round( Math.Round( Math.Round( Math.Round( Math.Round( Math.Round( Ma ...

  5. 在一个JSP页面中包含另一个JSP页面的三种方式

    转载自://http://blog.163.com/neu_lxb/blog/static/179417010201121343132918/ (1)include指令          includ ...

  6. PHP程序中删除字符串最后一个字符的三种方法

    常见的语法格式: foreach ($arr as $key => $value) {$arr_str = $arr['x_id'] . ',' . $arr_str;} 假设字符数组 $arr ...

  7. 拦截iOS系统导航栏返回按钮事件-三种方法

    方法一:在dealloc里面书写监听事件,因为只有pop才会调用dealloc,push不会掉用 - (void)dealloc {YLLog(@"123"); } 方法二:在- ...

  8. iOS边练边学--介绍布局的三种方法

    使用代码实现Autolayout的方法1- 创建约束 +(id)constraintWithItem:(id)view1attribute:(NSLayoutAttribute)attr1relate ...

  9. php中使用Curl、socket、file_get_contents三种方法POST提交数据

    抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看了一眼后感觉使用颇有些复杂,没有file_get_content那么简单,再就是需求 ...

随机推荐

  1. 20180929 北京大学 人工智能实践:Tensorflow笔记01

    北京大学 人工智能实践:Tensorflow笔记 https://www.bilibili.com/video/av22530538/?p=13 (完)

  2. 洛谷 P3199 [HNOI2009]最小圈

    P3199 [HNOI2009]最小圈 题目背景 如果你能提供题面或者题意简述,请直接在讨论区发帖,感谢你的贡献. 题目描述 对于一张有向图,要你求图中最小圈的平均值最小是多少,即若一个圈经过k个节点 ...

  3. ArcGIS api for javascript——放大时切换图层

    描述 本例展示了如何在地图里指出显示的缓存或切片的细节等级(LODs).当打开示例地图,可以看到一些来自ArcGIS Online ESRI_Imagery_World_2D图层的影像.这个应用程序配 ...

  4. Looger级别

    Logger级别 日志记录器(Logger)是日志处理的核心组件.log4j具有5种正常级别(Level).日志记录器(Logger)的可用级别Level (不包括自定义级别 Level), 以下内容 ...

  5. Python数据可视化——散点图

    PS: 翻了翻草稿箱. 发现竟然存了一篇去年2月的文章...尽管naive.还是发出来吧... 本文记录了python中的数据可视化--散点图scatter, 令x作为数据(50个点,每一个30维), ...

  6. Perl Learning 5 Hash

    [本文原创,未经同意请勿转载] 哈希是一种数据结构,它和数组的相似之处在于能够容纳随意多的值并能按需取用,而它和数组的不同在于索引方式,数组是以数字来索引.哈希则以名字来索引.也就是说.哈希的索引值, ...

  7. 在Mac OS X上安装使用lazarus 1.6.4

    一直觉得delphi的OO做得比C++还完善, 但如今日落西真是让人感到唏嘘,  这并不意味着delphi比C++差, 它的创始人被微软挖墙脚后创造了C#系列开发工具, 设计理念大部分与Delphi相 ...

  8. Android的矩阵(一):ColorMatrix

    最近的学习过程中看到关于android色彩矩阵的内容,以前看到这部分内容,基本都是跳过的,没有认真细读. 自己给自己找的借口是: 1,大一学的矩阵内容早就忘的干干净净了,当时学的时候就很烦人,所以现在 ...

  9. centos7 ssh免口令认证登录

    摘要:centos7, xshell, 公钥,  ssh ssh登录方式有口令认证登录和密钥认证登录 接下来本次介绍是ssh密钥登录方式 (1)产生公钥 (2)将公钥放置到centos7的(/root ...

  10. JWT 使用介绍

    转载收藏于 http://www.cnblogs.com/zjutzz/p/5790180.html JWT是啥? JWT就是一个字符串,经过加密处理与校验处理的字符串,形式为: A.B.C A由JW ...