IOS-navigation中左滑pop的三种方法

系统自带pop方法

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

关键代码

__weak typeof(self) weakSelf = self;

self.navigationController.interactivePopGestureRecognizer.delegate =

weakSelf;

下面是实例代码:

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

#import "AbeViewController.h"

@interface AbeViewController ()<UIGestureRecognizerDelegate>

@end

@implementation AbeViewController

- (void)viewDidLoad {
[super viewDidLoad];
} - (void)viewDidAppear:(BOOL)animated{
//**************方法一****************//
//设置滑动回退
__weak typeof(self) weakSelf = self; self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;
//判断是否为第一个view
if (self.navigationController && [self.navigationController.viewControllers count] == 1) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
} #pragma mark- UIGestureRecognizerDelegate
//**************方法一****************//
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
} @end

自定义边缘左滑手势方法

就是实现了一个手势方法,触发这个手势方法时pop。

下面是实例代码:

(继承AbeViewController类,就可以使用自定义边缘左滑手势的pop方法。)

#import "AbeViewController.h"

@interface AbeViewController ()<UIGestureRecognizerDelegate>

@end

@implementation AbeViewController

- (void)viewDidLoad {
[super viewDidLoad];
} - (void)viewDidAppear:(BOOL)animated{
//*************方法二*****************//
UIScreenEdgePanGestureRecognizer *edgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanGesture:)];
edgePanGestureRecognizer.delegate = self;
edgePanGestureRecognizer.edges = UIRectEdgeLeft;
[self.view addGestureRecognizer:edgePanGestureRecognizer];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} #pragma mark- private method
//*************方法二*****************//
- (void)edgePanGesture:(UIScreenEdgePanGestureRecognizer*)edgePanGestureRecognizer{
[self.navigationController popViewControllerAnimated:YES];
} #pragma mark- UIGestureRecognizerDelegate
//**************方法二****************//
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if (self.navigationController && [self.navigationController.viewControllers count] == 1) {
return NO;
}
return YES;
} @end

自定义view任何位置左移pop

在view中,任何位置左移触发pop方法。

其实就是建立了一个UIPanGestureRecognizer手势,然后该手势触发方法。

知识点:

[panGestureRecognizer locationInView:XX] 获取pan手势的CGPoint。

panGestureRecognizer.state pan的状态

self.interactivePopGestureRecognizer.enabled = NO; 原生左滑无效

下面是实例代码:

(继承ABENavViewController类,就可以使用自定义view左滑手势的pop方法; ABENavViewController为UINavigationController的子类)

#import "ABENavViewController.h"

@interface ABENavViewController ()

@property (strong, nonatomic)UIPanGestureRecognizer *panGestureRecognizer;
@property (strong, nonatomic)UIImageView *backView; @property (strong, nonatomic)NSMutableArray *backImgs;
@property (assign) CGPoint panBeginPoint;
@property (assign) CGPoint panEndPoint; @end @implementation ABENavViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//initlization
}
return self;
} - (void)loadView{
[super loadView]; [self initilization];
} - (void)viewDidLoad {
[super viewDidLoad];
[self loadBaseUI];
} - (void)initilization{
self.backImgs = [[NSMutableArray alloc] init];
} - (void)loadBaseUI{
//原生方法无效
self.interactivePopGestureRecognizer.enabled = NO; //设置手势
self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizerAction:)];
[self.view addGestureRecognizer:self.panGestureRecognizer];
} #pragma mark- public method
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
//截图
UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.size, YES, 1.0);
[[UIApplication sharedApplication].keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.backImgs addObject:img]; [super pushViewController:viewController animated:animated];
} - (UIViewController *)popViewControllerAnimated:(BOOL)animated{
[_backImgs removeLastObject]; return [super popViewControllerAnimated:animated];
} #pragma mark- private method
- (void)panGestureRecognizerAction:(UIPanGestureRecognizer*)panGestureRecognizer{
if ([self.viewControllers count] == 1) {
return ;
} if (panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
NSLog(@"滑动开始");
//存放滑动开始的位置
self.panBeginPoint = [panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow];
//插入图片
[self insertLastViewFromSuperView:self.view.superview]; }else if(panGestureRecognizer.state == UIGestureRecognizerStateEnded){
NSLog(@"滑动结束");
//存放数据
self.panEndPoint = [panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow]; if ((_panEndPoint.x - _panBeginPoint.x) > 50) {
[UIView animateWithDuration:0.3 animations:^{
[self moveNavigationViewWithLenght:[UIScreen mainScreen].bounds.size.width];
} completion:^(BOOL finished) {
[self removeLastViewFromSuperView];
[self moveNavigationViewWithLenght:0];
[self popViewControllerAnimated:NO];
}]; }else{
[UIView animateWithDuration:0.3 animations:^{
[self moveNavigationViewWithLenght:0];
}];
}
}else{
//添加移动效果
CGFloat panLength = ([panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow].x - _panBeginPoint.x);
if (panLength > 0) {
[self moveNavigationViewWithLenght:panLength];
}
} } /**
* 移动视图界面
*
* @param lenght 移动的长度
*/
- (void)moveNavigationViewWithLenght:(CGFloat)lenght{ //图片位置设置
self.view.frame = CGRectMake(lenght, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
//图片动态阴影
_backView.alpha = (lenght/[UIScreen mainScreen].bounds.size.width)*2/3 + 0.33;
} /**
* 插图上一级图片
*
* @param superView 图片的superView
*/
- (void)insertLastViewFromSuperView:(UIView *)superView{
//插入上一级视图背景
if (_backView == nil) {
_backView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_backView.image = [_backImgs lastObject];;
}
[self.view.superview insertSubview:_backView belowSubview:self.view];
} /**
* 移除上一级图片
*/
- (void)removeLastViewFromSuperView{
[_backView removeFromSuperview];
_backView = nil;
} @end

#IOS-navigation中左滑pop的三种方法的更多相关文章

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

    第三中类型.自己定义任何位置返回页面的方式,上边的类就是.m,大家能够贴过去使用.这个类是继承NavigationController的.用这个类初始化rootController就能够了.这里还有源 ...

  2. iOS 判断当前网络状态的三种方法

    http://www.cocoachina.com/ios/20171103/21039.html 在项目中,为了好的用户体验,有些场景必须线判断网络状态,然后才能决定改干嘛.比如视频播放,需要线判断 ...

  3. iOS容易造成循环引用的三种场景

    iOS容易造成循环引用的三种场景  ARC已经出来很久了,自动释放内存的确很方便,但是并非绝对安全绝对不会产生内存泄露.导致iOS对象无法按预期释放的一个无形杀手是--循环引用.循环引用可以简单理解为 ...

  4. iOS 拨打电话三种方法

    小弟查了很多地方的关于iOS程序拨打电话,大都不全,今天我总结了三种方法,各有不同,拿来给大家分享,希望给大家有所帮助1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出 ...

  5. IOS 多线程,线程同步的三种方式

    本文主要是讲述 IOS 多线程,线程同步的三种方式,更多IOS技术知识,请登陆疯狂软件教育官网. 一般情况下我们使用线程,在多个线程共同访问同一块资源.为保护线程资源的安全和线程访问的正确性. 在IO ...

  6. iOS拨打电话(三种方法)

    iOS拨打电话(三种方法)  查了很多地方的关于iOS程序拨打电话,大都不全,今天我总结了三种方法,各有不同,拿来给大家分享,希望给大家有所帮助 1,这种方法,拨打完电话回不到原来的应用,会停留在通讯 ...

  7. iOS开发 跳转场景的三种方式

    iOS开发 跳转场景的三种方式 2012年10月17日, 15:32 假设A跳转到B,三种方法:1.按住ctrl键,拖动A上的控件(比如说UIButton)到B上,弹出菜单,选择Modal.不需要写任 ...

  8. Chrome模拟手机浏览器(iOS/Android)的三种方法,亲测无误!

    大网站都有推出自己的手机访问版本页面,不管是新闻类还是视频网站,我们在电脑是无法直接访问到手机网站的,比如我经常访问一个3g.qq.com这个手机站点,如果在电脑上直接打开它,则会跳转到其它页面,一般 ...

  9. 用Fiddler可以设置浏览器的UA 和 手动 --Chrome模拟手机浏览器(iOS/Android)的三种方法,亲测无误!

    附加以一种软件的方法是:用Fiddler可以设置浏览器的UA 以下3种方法是手动的 通过伪装User-Agent,将浏览器模拟成Android设备. 第一种方法:新建Chrome快捷方式 右击桌面上的 ...

随机推荐

  1. 编程思想—面向切面编程(AOP)

    谈到面向切面的编程,我们很容易关联到面向对象编程(OOP).个人对这两种编程方式的解释为:两种编程思想只是站在编程的角度问题. OOP注重的是对象,怎么对对象行为和方法的抽象.如何封装一个具有完整属性 ...

  2. 将小度WiFi改造为无线网卡(小度WiFi能够接收WiFi信号)

    安装官方的小度WiFi的驱动器,只能让它当做无线信号的发射装置,但是我想通过小度WiFi让我的台式电脑能都接收无线信号,于是经过一番折腾终于成功了.我的是win7. 小度WiFi无法接受无线信号,不能 ...

  3. 2016最新Java笔试题集锦

    更新时间:2015-08-13         来源:网络         投诉删除 [看准网(Kanzhun.com)]笔试题目频道小编搜集的范文“2016最新Java笔试题集锦”,供大家阅读参考, ...

  4. 树莓派连接GPS模块

    一月份的时候觉得好玩买了树莓派,但是太懒没怎么研究,但最近当初买树莓派时的那个梦想又萦绕心头,决定抽空完成一下当年的计划~ GPS模块是其中很重要的一环,于是在某宝上搜索,找了一家相对便宜也很轻巧的G ...

  5. sqlserver 学习

    http://www.cnblogs.com/CareySon/category/411344.html SQL Server查找的最小单位实际上是页.也就是说即使你只查找一行很小的数据,SQL Se ...

  6. spm使用之五修改spm自带文档主题模板

    spm自带的文档的主题模板, 其文件在C:\Documents and Settings\Administrator\.spm\themes 目录下, 有个叫做cmd 文件夹的. 其实 cmd这个文件 ...

  7. BZOJ 1003 物流运输trans

    Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...

  8. Html和JS基础

    1.body:bgcolor,background(背景图片),bgproperities=fixed(图片水印),text(正文颜色). 2.hr:水平分割线,正文标题<h?>自动换行了 ...

  9. struts2不能通过ONGL方式取出request中的Atrribute

    请看下面一个很简单的Action package com.ahgw.main.action; import org.springframework.stereotype.Controller; /** ...

  10. Git命令详解

    一个中文git手册:http://progit.org/book/zh/ 原文:http://blog.csdn.net/sunboy_2050/article/details/7529841 前面两 ...