//
//  YPPopAnimator.m
//  YPReusableController
//
//  Created by MichaelPPP on 16/1/27.
//  Copyright 漏 2016骞?tyiti. All rights reserved.
//

#import "YPPopAnimator.h"
#import "YPReusableControllerConst.h"

@interface YPPopAnimator ()

@property (nonatomic, weak) UIView *blackMask;

@end

@implementation YPPopAnimator

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    // 鐩爣鎺у埗鍣?    UIViewController *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    // 婧愭帶鍒跺櫒
    UIViewController *fromVc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    // 鎺у埗鍣ㄦ爤
    UIView *container = [transitionContext containerView];
    
    [container insertSubview:toVc.view belowSubview:fromVc.view];
    
    UIView *blackMask = [[UIView alloc] init];
    self.blackMask = blackMask;
    blackMask.backgroundColor = YPBlackColor;
    blackMask.frame = CGRectMake(0, 0, YPScreenW, YPScreenH);
    [container insertSubview:self.blackMask aboveSubview:toVc.view];
    
    float scale = 0.95;
    toVc.view.transform = CGAffineTransformMakeScale(scale, scale);
    self.blackMask.alpha = 0.4;
    fromVc.view.yp_x = 0;
    
    __weak typeof(self) weakSelf = self;
    [UIView animateKeyframesWithDuration:self.duration delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        // 鍔ㄧ敾鏁堟灉
        toVc.view.transform = CGAffineTransformIdentity;
        fromVc.view.yp_x = YPScreenW;
        weakSelf.blackMask.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf.blackMask removeFromSuperview];
        [fromVc.view removeFromSuperview];
        if (transitionContext.transitionWasCancelled) { // 濡傛灉閬囧埌鏈煡鍙栨秷鎿嶄綔鎭㈠鏍堢粨鏋?            [container addSubview:fromVc.view];
        }
        [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
    }];
}

@end
// 鐗堟潈灞炰簬鍘熶綔鑰?// http://code4app.com (cn) http://code4app.net (en)
// 鍙戝竷浠g爜浜庢渶涓撲笟鐨勬簮鐮佸垎浜綉绔? Code4App.com