小胖说事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那么简单,再就是需求 ...
随机推荐
- springboot框架笔记——springboot提供的自动配置
Springboot基本配置 spring MVC的定制配置需要我们的配置实现一个WebMvcConfigurer接口,如果实在spring环境下需要使用@EnableWebMVC注解,来开启对spr ...
- 题解 P2195 【HXY造公园】
天哪这道题竟然只有一篇题解! emm,首先读题看完两个操作就已经有很明确的思路了,显然是并查集+树的直径 一波解决. 并查集不多说了,如果不了解的可以看这里. 树的直径的思路很朴实,就是两边DFS(B ...
- [Recompose] Refactor React Render Props to Streaming Props with RxJS and Recompose
This lesson takes the concept of render props and migrates it over to streaming props by keeping the ...
- 《Java并发编程实战》第五章 同步容器类 读书笔记
一.同步容器类 1. 同步容器类的问题 线程容器类都是线程安全的.可是当在其上进行符合操作则须要而外加锁保护其安全性. 常见符合操作包括: . 迭代 . 跳转(依据指定顺序找到当前元素的下一个元素) ...
- 什么是 "署名-非商业性使用-同样方式共享"
什么是 "署名-非商业性使用-同样方式共享" 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致& ...
- Unity 之 C# 利用回调函数实现C++匿名函数
做C++开发的都用过匿名函数很好用,可是C#开发怎么实现呢?前几天做一个拍照功能的时候.我偶然发现某个函数假设是C++的话.用匿名函数太好了,于是開始研究C#的回调,代理.托付等,最后总算是实现了我想 ...
- 【HDU 5402】Travelling Salesman Problem(构造)
被某题卡SB了,结果这题也没读好...以为每一个格子能够有负数就当搜索做了.怎么想也搜只是去,后来发现每一个格子是非负数,那么肯定就是构造题. 题解例如以下: 首先假设nn为奇数或者mm为奇数,那么显 ...
- 巧用FPGA中资源
随着FPGA的广泛应用,所含的资源也越来越丰富,从基本的逻辑单元.DSP资源和RAM块,甚至CPU硬核都能集成在一块芯片中.在做FPGA设计时,如果针对FPGA中资源进行HDL代码编写,对设计的资源利 ...
- 洛谷P2598 [ZJOI2009]狼和羊的故事
题目描述 “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想:狼和羊如此和谐,为什么不尝试羊狼合养呢?说干就干! Orez的羊狼圈 ...
- Day2代码
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #defi ...