zoukankan      html  css  js  c++  java
  • 小胖说事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
    

    
    




  • 相关阅读:
    Jenkins服务器磁盘空间爆满问题解决
    U3D GPU蒙皮
    关于STRUCT优化的一个点
    UNITY优化资料收集
    U3D的结构体堆分配栈分配
    【转】UGUI研究院之缓存策略让UI打开更快(三十)
    关于U3D场景烘焙的一个想法
    【摘】如果医生给你的孩子开这些药,请主动说不!
    【转】投影矩阵的推导
    Optimizing graphics performance
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/7039341.html
Copyright © 2011-2022 走看看