本文转载自:点击打开链接
第三中类型,自定义任意位置返回页面的方式,上边的类就是.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