小胖说事29-----iOS中Navigation中左滑pop页面的三种方法
来源:互联网 发布:linux informix 安装 编辑:程序博客网 时间:2024/06/11 18:55
第三中类型,自定义任意位置返回页面的方式,上边的类就是.m,大家可以贴过去使用,这个类是继承NavigationController的,用这个类初始化rootController就可以了,这里还有源码可下载,完整的类:http://download.csdn.net/detail/haogaoming123/8906671
1.系统自带pop方法">系统自带pop方法
如果我们没有对navigation中的back按钮进行自定义,我们可以直接使用系统自带的左滑pop方法。但是如果我们对back按钮,进行了自定义,我们就要对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
0 0
- 小胖说事29-----iOS中Navigation中左滑pop页面的三种方法
- 小胖说事29-----iOS中Navigation中左滑pop页面的三种方法
- iOS-navigation中左滑pop的三种方法
- IOS-navigation中左滑pop的三种方法(转)
- navigation中左滑pop的三种方法
- iOS tabbar和navigation组合,点击item两次,会pop到上一个页面,解决不让控制器跳转的方法
- iOS pop到指定的页面
- 小学ios:navigation push的几种方法
- iOS 重写pop跳转的方法
- ios中navigation相关的视图切换
- ios中navigation controller的漂移问题
- jsp页面中插入css样式的三种方法
- 页面显示数据库中数据的三种方法
- jsp页面中插入css样式的三种方法
- struts2中jsp页面向后台传值的三种方法
- jsp页面中插入css样式的三种方法
- JSP页面中插入css样式的三种方法
- iOS pop至指定页面
- fidder手机抓包
- flatbuffers C++ 宏代码欣赏
- 【直播预告】7月19日iOS免费公开课答疑,参与送C币!
- android按Menu出现菜单的动画
- Longest Palindromic Substring
- 小胖说事29-----iOS中Navigation中左滑pop页面的三种方法
- Caused by: java.lang.NoClassDefFoundError: org/springframework/web/context/WebApplicationContext
- C#和.Net联系
- 【机房重构】SQL语句已终止
- Median of Two Sorted Arrays
- 每天一个小知识点3(原型与构造函数总结一)
- 冒泡方排序
- Linux下执行ping命令创建得到的进程的有效用户不是root
- noi模板-HDU1269-强连通分量