iOS-自定义侧滑返回功能

系统自带的侧滑返回只能从边缘开始,现在手机屏幕越来越大,作为一个单手操作很不习惯,为了用户体验,于是我自定义了侧滑返回功能,在下用的是截屏+重写push和pop以及popToRoot,只要继承我写的BBNavigationController这个类就能在全局使用,简单适用(modal出来的抱歉,不支持,仅适用于导航控制器)。对于项目架构的是侧边栏的直接私我,这里需要处理一下截图与侧边栏冲突的问题,这里不作详细处理。废话不多说上代码。
1.添加手势

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    
    [self.view addGestureRecognizer:panGesture];
    
    isMoving = NO;
    
}

2.重写push方法,这里我设置了所有2级子控制器的的返回键,不想要的自行去掉。

#pragma mark - 重写push
/**
 *  重写push,是为了截张图片。还有重写navgationItem按钮并监听
 */

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count > 0) {
        /* 设置导航栏上面的内容 */
        // 设置左边的返回按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 30, 30);
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 5)];
        [btn setImage:[UIImage imageNamed:@"nav_return"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        
    } else {
        
    }
    
    [self.screenShotsList addObject:[self ViewRenderImage]];
    
    [super pushViewController:viewController animated:animated];
}

/**
 *  截图
 */
- (UIImage *)ViewRenderImage
{
    //这里如果传window的size会报错,因为程序刚启动的时候界面是push进来的,此时window还没有值,
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(KScreenWidth, KScreenHeight), YES, 0.0);
    [WINDOW.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

- (void)back
{
    // 因为self本来就是一个导航控制器,self.navigationController这里是nil的
    [self popViewControllerAnimated:YES];
}

3.重写pop

#pragma mark -重写pop方法

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    
    UIViewController *popVC = nil;
    
    if (animated) {
        
        if (self.childViewControllers.count > 0) {
            
            [self addLastScreenShotView];
        }
        
        //先缩放图片 kScaleValue
        lastScreenShotView.transform = CGAffineTransformMakeScale(kScaleValue, kScaleValue);
        
        [UIView animateWithDuration:kDurationTime animations:^{
            
            [self moveViewWithX:KScreenWidth];
            self.cover.alpha = 0;
        } completion:^(BOOL finished) {
            
            [super popViewControllerAnimated:NO];
            CGRect frame = self.view.frame;
            frame.origin.x = 0;
            self.view.frame = frame;
            self.cover.alpha = kCoverAphla;
            [self.screenShotsList removeLastObject];
            if (lastScreenShotView) [lastScreenShotView removeFromSuperview];
            
        }];
        
        popVC = [self.viewControllers lastObject];
        
    }else{
        
        popVC = [super popViewControllerAnimated:NO];
        
    }
    return popVC;
}

/**
 *  添加背景图片到window上
 */
- (void)addLastScreenShotView
{
    
    UIImage *lastScreenShot = [self.screenShotsList lastObject];
    
    if (lastScreenShotView) [lastScreenShotView removeFromSuperview];
    
    lastScreenShotView = [[UIImageView alloc] initWithImage:lastScreenShot];
    
    [WINDOW insertSubview:lastScreenShotView atIndex:0];
    
    [WINDOW insertSubview:self.cover aboveSubview:lastScreenShotView];
    
}

4、需要重写popToRoot

#pragma mark - 重写popToRoot
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated {
    
    if (self.childViewControllers.count > 0) {
        
        [self addScreenShotIsTheFirst:YES];
    }
    
    //先缩放图片 kScaleValue
    lastScreenShotView.transform = CGAffineTransformMakeScale(kScaleValue, kScaleValue);
    
    [UIView animateWithDuration:kDurationTime animations:^{
        
        [self moveViewWithX:[UIScreen mainScreen].bounds.size.width];
        self.cover.alpha = 0;
    } completion:^(BOOL finished) {
        
        [super popToRootViewControllerAnimated:NO];
        CGRect frame = self.view.frame;
        frame.origin.x = 0;
        self.view.frame = frame;
        self.cover.alpha = kCoverAphla;
        [self.screenShotsList removeAllObjects];
        if (lastScreenShotView) [lastScreenShotView removeFromSuperview];
        
    }];
    return self.childViewControllers;
}

5、手势处理

#pragma mark - 手势处理

- (void)handlePanGesture:(UIGestureRecognizer*)sender
{
    //顶级 controller 则不执行返回
    if(self.viewControllers.count <= 1){
        
        return;
        
    } else {
        
        //得到触摸中在window上拖动的过程中的xy坐标
        CGPoint translation = [sender locationInView:WINDOW];
        
        if (translation.x - startTouch.x < 0) {//禁止向左移动
            isMoving = NO;
        }
        
        if(sender.state == UIGestureRecognizerStateEnded){
            isMoving = NO;
            
            //如果结束坐标大于开始坐标100像素就动画效果移动
            if (translation.x - startTouch.x > 100) {
                [UIView animateWithDuration:kDurationTime animations:^{
                    
                    [self moveViewWithX:KScreenWidth];
                    self.cover.alpha = 0;
                    
                } completion:^(BOOL finished) {
                    
                    [self gesturePopViewControllerAnimated:NO];
                    //将 self.view 的 x 坐标重置为0
                    CGRect frame = self.view.frame;
                    frame.origin.x = 0;
                    self.view.frame = frame;
                    self.cover.alpha = kCoverAphla;
                    if (lastScreenShotView) [lastScreenShotView removeFromSuperview];
                    
                }];
                
            }else{
                
                //不大于100时就移动原位
                [UIView animateWithDuration:kDurationTime animations:^{
                    [self moveViewWithX:0];
                } completion:^(BOOL finished) {
                    
                }];
            }
        }else if(sender.state == UIGestureRecognizerStateBegan){
            
            startTouch = translation;
            isMoving = YES;
            [self addLastScreenShotView];
        }
        
        if (isMoving) {
            [self moveViewWithX:translation.x - startTouch.x];
        }
        
    }
    
}

- (UIViewController *)gesturePopViewControllerAnimated:(BOOL)animated
{
    [self.screenShotsList removeLastObject];
    return [self popViewControllerAnimated:animated];
}

- (void)moveViewWithX:(float)x
{
    x = x > KScreenWidth ? KScreenWidth : x;
    
    CGRect frame = self.view.frame;
    frame.origin.x = x;
    self.view.frame = frame;
    float scale;
    if (kScaleValue == 1) {
        scale = kScaleValue;//缩放大小
    } else {
        scale = (x / 6400) + kScaleValue;//缩放大小
    }
    
    if (x == KScreenWidth) {
        
        lastScreenShotView.transform = CGAffineTransformIdentity;
        
    }else{
        //缩放scale
        lastScreenShotView.transform = CGAffineTransformMakeScale(scale, scale);
    }
}

5.来发传送门 demo地址 ,喜欢的人点个star

你可能感兴趣的:(iOS-自定义侧滑返回功能)