美文网首页
iOS 侧滑pop的三种方法

iOS 侧滑pop的三种方法

作者: LucXion | 来源:发表于2017-09-15 16:06 被阅读0次
    • 方法一、系统自带pop方法

    如果我们没有对navigation中的back按钮进行自定义,我们可以直接使用系统自带的左滑pop方法。但是如果我们对back按钮,进行了自定义,我们就要对self.navigationController.interactivePopGestureRecognizer这个属性进行设置了

    关键代码

    __weak typeof(self) weakSelf = self;
    self.navigationController.interactivePopGestureRecognizer.delegate =
    weakSelf;
    
    // 遵守<UIGestureRecognizerDelegate>协议
    - (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;
    }
    
    • 方法二、自定义边缘左划手势
    - (void)viewDidAppear:(BOOL)animated{
        
        UIScreenEdgePanGestureRecognizer *edgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanGesture:)];
        edgePanGestureRecognizer.delegate = self;
        edgePanGestureRecognizer.edges = UIRectEdgeLeft;
        [self.view addGestureRecognizer:edgePanGestureRecognizer];
    }
    
    #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;
    }
    
    • 方法三、自定义全屏popView
    #import "NaViewController.h"
    
    @interface NaViewController ()
    
    @property (strong, nonatomic)UIPanGestureRecognizer *panGestureRecognizer;
    @property (strong, nonatomic)UIImageView *backView;
    
    @property (strong, nonatomic)NSMutableArray *backImgs;
    @property (assign) CGPoint panBeginPoint;
    @property (assign) CGPoint panEndPoint;
    
    @end
    
    @implementation NaViewController
    
    - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            //initlization
        }
        return self;
    }
    
    - (void)loadView{
        [super loadView];
        
        [self initilization];
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self loadBaseUI];
    }
    
    - (void)initilization{
        self.backImgs = [[NSMutableArray alloc] init];
    }
    
    - (void)loadBaseUI{
        
        
        self.view.backgroundColor = [UIColor grayColor];
        
        //原生方法无效
        self.interactivePopGestureRecognizer.enabled = NO;
        
        //设置手势
        self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizerAction:)];
        [self.view addGestureRecognizer:self.panGestureRecognizer];
    }
    
    #pragma mark- public method
    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
        //截图
        UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.size, YES, 1.0);
        [[UIApplication sharedApplication].keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [self.backImgs addObject:img];
        
        [super pushViewController:viewController animated:animated];
    }
    
    - (UIViewController *)popViewControllerAnimated:(BOOL)animated{
        [_backImgs removeLastObject];
        
        return [super popViewControllerAnimated:animated];
    }
    
    #pragma mark- private method
    - (void)panGestureRecognizerAction:(UIPanGestureRecognizer*)panGestureRecognizer{
        
        if ([self.viewControllers count] == 1) {
            return ;
        }
        
        if (panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
            NSLog(@"滑动开始");
            //存放滑动开始的位置
            self.panBeginPoint = [panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow];
            //插入图片
            [self insertLastViewFromSuperView:self.view.superview];
            
        }else if(panGestureRecognizer.state == UIGestureRecognizerStateEnded){
            NSLog(@"滑动结束");
            //存放数据
            self.panEndPoint = [panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow];
            
            if ((_panEndPoint.x - _panBeginPoint.x) > 50) {
                [UIView animateWithDuration:0.3 animations:^{
                    [self moveNavigationViewWithLenght:[UIScreen mainScreen].bounds.size.width];
                } completion:^(BOOL finished) {
                    [self removeLastViewFromSuperView];
                    [self moveNavigationViewWithLenght:0];
                    [self popViewControllerAnimated:NO];
                }];
                
                
            }else{
                [UIView animateWithDuration:0.3 animations:^{
                    [self moveNavigationViewWithLenght:0];
                }];
            }
        }else{
            //添加移动效果
            CGFloat panLength = ([panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow].x - _panBeginPoint.x);
            if (panLength > 0) {
                [self moveNavigationViewWithLenght:panLength];
            }
        }
        
    }
    
    /**
     *  移动视图界面
     *
     *  @param lenght 移动的长度
     */
    - (void)moveNavigationViewWithLenght:(CGFloat)lenght{
        
        //图片位置设置
        self.view.frame = CGRectMake(lenght, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
        //图片动态阴影
        _backView.alpha = (lenght / [UIScreen mainScreen].bounds.size.width) * 2/3 + 0.33;
    }
    
    /**
     *  插图上一级图片
     *
     *  @param superView 图片的superView
     */
    - (void)insertLastViewFromSuperView:(UIView *)superView{
        //插入上一级视图背景
        if (_backView == nil) {
            _backView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
            _backView.image = [_backImgs lastObject];;
        }
        [self.view.superview insertSubview:_backView belowSubview:self.view];
    }
    
    /**
     *  移除上一级图片
     */
    - (void)removeLastViewFromSuperView{
        [_backView removeFromSuperview];
        _backView = nil;
    }
    
    

    相关文章

      网友评论

          本文标题:iOS 侧滑pop的三种方法

          本文链接:https://www.haomeiwen.com/subject/cgissxtx.html