小胖说事29-----iOS中Navigation中左滑pop页面的三种方法
第三中类型。自己定义任何位置返回页面的方式,上边的类就是.m,大家能够贴过去使用。这个类是继承NavigationController的。用这个类初始化rootController就能够了。这里还有源代码可下载。完整的类:http://download.csdn.net/detail/haogaoming123/8906671
1.系统自带pop方法">系统自带pop方法
假设我们没有对navigation中的backbutton进行自己定义,我们能够直接使用系统自带的左滑pop方法。
可是假设我们对backbutton。进行了自己定义,我们就要对self.navigationController.interactivePopGestureRecognizer
这个属性进行设置了。关键代码:
- __weak typeof(self) weakSelf = self;
- self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;
以下是实例代码:
(继承AbeViewController类。就能够使用系统自带的pop方法。)
- @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
2.自己定义边缘左滑手势方法
以下是实例代码:
就是实现了一个手势方法,触发这个手势方法时pop。(继承AbeViewController类,就能够使用自己定义边缘左滑手势的pop方法。
)
- @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
3.自己定义view不论什么位置左移pop
事实上就是建立了一个UIPanGestureRecognizer手势,然后该手势触发方法。panGestureRecognizer.state pan的状态。
而且设置self.interactivePopGestureRecognizer.enabled = NO; 原生左滑无效
以下是实例代码:在view中,不论什么位置左移触发pop方法。
知识点:[panGestureRecognizer locationInView:XX] 获取pan手势的CGPoint。
(继承ABENavViewController类,就能够使用自己定义view左滑手势的pop方法; ABENavViewController为UINavigationController的子类。
- //
- // NavigationViewController.m
- // BaseProject
- //
- // Created by haogaoming on 15/7/13.
- // Copyright (c) 2015年 郝高明. All rights reserved.
- //
- #import "NavigationViewController.h"
- @interface NavigationViewController ()
- @property (nonatomic,strong) UIImageView *backview;
- @property (nonatomic,strong) NSMutableArray *backImgs;
- @property (nonatomic,assign) CGPoint panBeginPoint;
- @property (nonatomic,assign) CGPoint panEndPoint;
- @end
- @implementation NavigationViewController
- - (id)init {
- self = [super init];
- self.delegate = self;
- return self;
- }
- - (id)initWithRootViewController:(UIViewController *)rootViewController {
- self = [super initWithRootViewController:rootViewController];
- self.delegate = self;
- return self;
- }
- -(void)loadView
- {
- [super loadView];
- self.backImgs = [NSMutableArray array];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- //原生方法无效
- self.interactivePopGestureRecognizer.enabled = NO;
- //设置手势
- [self.view AddGestureRecognizer:UIPanGestureRecognizerStyle delegate:self Section:@selector(panGestureRecognizerAction:)];
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
- {
- return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
- }
- - (BOOL)shouldAutorotate
- {
- return NO;
- }
- -(UIViewController *)popViewControllerAnimated:(BOOL)animated
- {
- [_backImgs removeLastObject];
- return [super popViewControllerAnimated:animated];
- }
- - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
- if (self.viewControllers.count==1) {
- viewController.hidesBottomBarWhenPushed = YES;
- }
- //截图
- UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.size, YES, 0.0f);
- [[UIApplication sharedApplication].keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
- UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- [self.backImgs addObject:img];
- sleep(0.5); //防止没有截频成功
- [super pushViewController:viewController animated:animated];
- }
- #pragma make-method
- -(void)panGestureRecognizerAction:(UIPanGestureRecognizer *)panGesture
- {
- if (self.viewControllers.count == 1) {
- return;
- }
- if (panGesture.state == UIGestureRecognizerStateBegan) {
- //滑动開始
- self.panBeginPoint = [panGesture locationInView:[UIApplication sharedApplication].keyWindow];
- //插入图片
- [self insertLastViewFromSuperView:self.view.superview];
- }else if (panGesture.state == UIGestureRecognizerStateEnded){
- //滑动结束
- self.panEndPoint = [panGesture locationInView:[UIApplication sharedApplication].keyWindow];
- if (self.view.left >= (self.view.width/2.0)-50) {
- [UIView animateWithDuration:0.3 animations:^{
- [self moveNavigationViewWithLength:[UIScreen mainScreen].bounds.size.width];
- } completion:^(BOOL finished) {
- [self removeLastViewFromSuperview];
- [self moveNavigationViewWithLength:0];
- [self popViewControllerAnimated:NO];
- }];
- }else{
- [UIView animateWithDuration:0.3 animations:^{
- [self moveNavigationViewWithLength:0];
- }];
- }
- }else{
- CGPoint point = [panGesture locationInView:[UIApplication sharedApplication].keyWindow];
- //防止右滑
- if ((point.x-self.panBeginPoint.x)<0) {
- return;
- }
- [self moveNavigationViewWithLength:(point.x-self.panBeginPoint.x)];
- }
- }
- /**
- * 更改frame的位置
- *
- * @param lenght self.view的left位置
- */
- -(void)moveNavigationViewWithLength:(CGFloat)lenght
- {
- //图片位置设置
- self.view.frame = CGRectMake(lenght, self.view.top, self.view.width, self.view.height);
- //图片动态阴影
- _backview.alpha = (lenght/[UIScreen mainScreen].bounds.size.width)*2/3 + 0.5;
- }
- /**
- * 将背景图插入到当前view的下边。然后通过改变self.view的frame实现滑动
- *
- * @param supView 画布
- */
- -(void)insertLastViewFromSuperView:(UIView *)supView
- {
- //插入上一级视图背景
- if (_backview == nil) {
- _backview = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
- } _backview.image = [_backImgs lastObject];
- [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
小胖说事29-----iOS中Navigation中左滑pop页面的三种方法的更多相关文章
- VC中加载LIB库文件的三种方法
VC中加载LIB库文件的三种方法 在VC中加载LIB文件的三种方法如下: 方法1:LIB文件直接加入到工程文件列表中 在VC中打开File View一页,选中工程名,单击鼠标右键,然后选中&quo ...
- Linux 中执行Shell 脚本的方式(三种方法)
Shell 脚本的执行方式通常有如下三种: (1)bash script-name 或者 sh script-name:(2)path/script-name或者./script-name:(3)so ...
- Java中实现十进制数转换为二进制的三种方法
第一种:除基倒取余法 这是最符合我们平时的数学逻辑思维的,即输入一个十进制数n,每次用n除以2,把余数记下来,再用商去除以2...依次循环,直到商为0结束,把余数倒着依次排列,就构成了转换后的二进制数 ...
- C#中Math类的计算整数的三种方法
1.Math.Round:四舍六入五取偶 引用内容 Math.Round( Math.Round( Math.Round( Math.Round( Math.Round( Math.Round( Ma ...
- 在一个JSP页面中包含另一个JSP页面的三种方式
转载自://http://blog.163.com/neu_lxb/blog/static/179417010201121343132918/ (1)include指令 includ ...
- PHP程序中删除字符串最后一个字符的三种方法
常见的语法格式: foreach ($arr as $key => $value) {$arr_str = $arr['x_id'] . ',' . $arr_str;} 假设字符数组 $arr ...
- 拦截iOS系统导航栏返回按钮事件-三种方法
方法一:在dealloc里面书写监听事件,因为只有pop才会调用dealloc,push不会掉用 - (void)dealloc {YLLog(@"123"); } 方法二:在- ...
- iOS边练边学--介绍布局的三种方法
使用代码实现Autolayout的方法1- 创建约束 +(id)constraintWithItem:(id)view1attribute:(NSLayoutAttribute)attr1relate ...
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看了一眼后感觉使用颇有些复杂,没有file_get_content那么简单,再就是需求 ...
随机推荐
- 20180929 北京大学 人工智能实践:Tensorflow笔记01
北京大学 人工智能实践:Tensorflow笔记 https://www.bilibili.com/video/av22530538/?p=13 (完)
- 洛谷 P3199 [HNOI2009]最小圈
P3199 [HNOI2009]最小圈 题目背景 如果你能提供题面或者题意简述,请直接在讨论区发帖,感谢你的贡献. 题目描述 对于一张有向图,要你求图中最小圈的平均值最小是多少,即若一个圈经过k个节点 ...
- ArcGIS api for javascript——放大时切换图层
描述 本例展示了如何在地图里指出显示的缓存或切片的细节等级(LODs).当打开示例地图,可以看到一些来自ArcGIS Online ESRI_Imagery_World_2D图层的影像.这个应用程序配 ...
- Looger级别
Logger级别 日志记录器(Logger)是日志处理的核心组件.log4j具有5种正常级别(Level).日志记录器(Logger)的可用级别Level (不包括自定义级别 Level), 以下内容 ...
- Python数据可视化——散点图
PS: 翻了翻草稿箱. 发现竟然存了一篇去年2月的文章...尽管naive.还是发出来吧... 本文记录了python中的数据可视化--散点图scatter, 令x作为数据(50个点,每一个30维), ...
- Perl Learning 5 Hash
[本文原创,未经同意请勿转载] 哈希是一种数据结构,它和数组的相似之处在于能够容纳随意多的值并能按需取用,而它和数组的不同在于索引方式,数组是以数字来索引.哈希则以名字来索引.也就是说.哈希的索引值, ...
- 在Mac OS X上安装使用lazarus 1.6.4
一直觉得delphi的OO做得比C++还完善, 但如今日落西真是让人感到唏嘘, 这并不意味着delphi比C++差, 它的创始人被微软挖墙脚后创造了C#系列开发工具, 设计理念大部分与Delphi相 ...
- Android的矩阵(一):ColorMatrix
最近的学习过程中看到关于android色彩矩阵的内容,以前看到这部分内容,基本都是跳过的,没有认真细读. 自己给自己找的借口是: 1,大一学的矩阵内容早就忘的干干净净了,当时学的时候就很烦人,所以现在 ...
- centos7 ssh免口令认证登录
摘要:centos7, xshell, 公钥, ssh ssh登录方式有口令认证登录和密钥认证登录 接下来本次介绍是ssh密钥登录方式 (1)产生公钥 (2)将公钥放置到centos7的(/root ...
- JWT 使用介绍
转载收藏于 http://www.cnblogs.com/zjutzz/p/5790180.html JWT是啥? JWT就是一个字符串,经过加密处理与校验处理的字符串,形式为: A.B.C A由JW ...