iOS全屏返回加截屏特效

看了一些app的返回效果,自己忍不住抽时间研究了一下,demo:FullScreenReturn

动画效果:·········································································································


demo.gif

核心代码

  • 在NavViewController中重写下面两个方法,我们就可以自定义想要的返回效果
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    UIImage *image = [self currentScreenCaptureImage];
    if (image) {
        [self.screenCaptureList addObject:image];
    }
    [super pushViewController:viewController animated:animated];
    
    [self resetPanGestureEnableStatus];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    [self.screenCaptureList removeLastObject];
    UIViewController *VC = [super popViewControllerAnimated:animated];
    if ([VC isKindOfClass:[self.topViewController class]]) {
        [self.screenCaptureList removeAllObjects];
    }
    [self resetPanGestureEnableStatus];
    return VC;
}
  • 创建并获取当前屏幕的视图,也就是截屏
- (UIImage *)currentScreenCaptureImage {
    UIView *view = self.view.superview;
    if (self.tabBarController) {
        view = self.tabBarController.view;
    }
    if (!view || CGRectIsEmpty(view.frame)) {
        return nil;
    }
    
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);

    [view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull subView, NSUInteger idx, BOOL * _Nonnull stop) {
        [subView drawViewHierarchyInRect:subView.frame afterScreenUpdates:NO];
    }];
    UIImage *screenCaptureImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenCaptureImage;
}

UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);


创建一个基于位图的上下文(context),并将其设置为当前上下文(context)。
size: 新创建的位图上下文的大小。也是UIGraphicsGetImageFromCurrentImageContext()返回的image大小。
opaque: 透明开关,一般设置为YES,如果图形不用透明,设置为YES以优化位图的存储,渲染效率更高。
scale 缩放比例, 一般设置为0,系统会自动设置正确的比例。iPhone 4是2.0,其他是1.0。

  • 添加拖动手势
- (void)addPanGestureRecognizer {
    self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizerAction:)];
    self.panGestureRecognizer.delegate = self;
    [self.view addGestureRecognizer:self.panGestureRecognizer];
}
  • 自定义返回效果(行间有注释)
- (void)panGestureRecognizerAction:(UIPanGestureRecognizer *)panGestureRecognizer {
    //触点位置
    CGPoint touchPoint = [panGestureRecognizer locationInView:[[UIApplication sharedApplication] keyWindow]];
    
    switch (panGestureRecognizer.state) {
        case UIGestureRecognizerStateBegan: {
            //触点开始位置
            self.startTouchPoint = touchPoint;
            
            //将上一个屏幕的截图放在当前view的下面
            self.screenCaptureImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
            UIImage *lastScreenCaptureImage = [self.screenCaptureList lastObject];
            [self.screenCaptureImageView setImage:lastScreenCaptureImage];
            
            self.blackMaskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
            [self.backgroundView insertSubview:self.screenCaptureImageView belowSubview:self.blackMaskView];
            
            self.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
            [self.view.superview insertSubview:self.backgroundView belowSubview:self.view];
            [self.backgroundView addSubview:self.blackMaskView];
        }
            break;
        case UIGestureRecognizerStateChanged: {
            //根据拖动的距离实现动画效果
            CGFloat displacement = touchPoint.x - self.startTouchPoint.x;
            [self moveViewWithDisplacement:displacement > 0 ? displacement : 0];
        }
            break;
        case UIGestureRecognizerStateEnded: {
            //根据设定的距离判断是否返回,并作出相应处理
            CGFloat displacement = touchPoint.x - self.startTouchPoint.x;
            if (displacement > moveWidth) {
                [UIView animateWithDuration:animateDuration animations:^{
                    [self moveViewWithDisplacement:[UIScreen mainScreen].bounds.size.width];
                } completion:^(BOOL finished) {
                    [self popViewControllerAnimated:NO];
                    self.view.leftX = 0;
                    self.backgroundView.hidden = YES;
                }];
            } else {
                [UIView animateWithDuration:animateDuration animations:^{
                    [self moveViewWithDisplacement:0];
                } completion:^(BOOL finished) {
                    self.backgroundView.hidden = YES;
                }];
            }
        }
            break;
        //其他异常情况处理
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
        case UIGestureRecognizerStatePossible:
        {
            [UIView animateWithDuration:animateDuration animations:^{
                 [self moveViewWithDisplacement:0];
             } completion:^(BOOL finished) {
                 self.backgroundView.hidden = YES;
             }];
        }
            break;
        default:
            break;
    }
}
  • 根据拖动的距离实现动画效果
- (void)moveViewWithDisplacement:(CGFloat)displacement {
    CGFloat width = self.view.width;
    displacement = displacement <= width ? displacement : 0;
    displacement = displacement > 0 ? displacement : 0;
    self.view.leftX = displacement;
    
    CGFloat scale = displacement / (width * 20) + 0.95f;
    CGFloat alpha = 0.4f - displacement / 800;
    
    self.screenCaptureImageView.transform = CGAffineTransformMakeScale(scale, scale);
    self.blackMaskView.alpha = alpha;
}

其他细节问题,可能要根据不同App的业务逻辑自行实现。其实很简单。

你可能感兴趣的:(iOS全屏返回加截屏特效)