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. 关于微软RDLC报表打印时文字拉伸问题(Windows server 2003 sp2)

    最近我们开发的打印服务频频出现打印文字拉伸问题,客户意见络绎不绝,最为明显的是使用黑体加粗后 “2.0份” 打印出来后小数点几乎看不见了,用户很容易误认为 “ 20份” .所以问题达到了不得不停下手上 ...

  2. Java POI 导出EXCEL经典实现 Java导出Excel

    转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...

  3. 安卓天天练练(十一)用list绑数据

    之前在练练ListView的时候就有个 BaseAdapter ba = new BaseAdapter() { 用Adapter来绑数据的方法,该方法中 LinearLayout ll=new Li ...

  4. KMP字符串模式匹配详解(转)

    来自CSDN     A_B_C_ABC 网友 KMP字符串模式匹配通俗点说就是一种在一个字符串中定位另一个串的高效算法.简单匹配算法的时间复杂度为O(m*n);KMP匹配算法.可以证明它的时间复杂度 ...

  5. xstream 别名的用法<转>

    1.xstream的alias使用方法: 1.1 作用:将序列化中的类全量名称,用别名替换. 1.2  使用方法:xstream.alias("blog", Blog.class) ...

  6. Condition 的使用

    Condition 将 Object 监视器方法(wait.notify 和 notifyAll)分解成截然不同的对象,以便通过将这些对象与任意 Lock 实现组合使用,为每个对象提供多个等待 set ...

  7. 两种QMultiMap的遍历方法(最好使用只读遍历器)

    留个爪,备查 QMultiMap<QString, QString>& remote_map = my_obj->m_MapVersion; // ccc 这里体现了引用的好 ...

  8. Distinct Subsequences——Leetcode

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  9. .Net remoting 的文件传输

    http://www.codeproject.com/Articles/14100/Dot-Net-Remoting-Handling-Remote-Events-using-Dele

  10. windows调用ubuntu下的sublimeText2环境搭建

    部署需求: windows: windows 7 32 sp1 32位: linux :ubuntu 12.04 LTS 64位: 环境: windows安装:xmanager 4 linux安装:g ...