CNUpSlideAnimationTransition.swift 3.68 KB
//
//  CNUpSlideAnimationTransition.swift
//  metaCode
//
//  Created by zx on 2023/10/16.
//

import UIKit

fileprivate let transitonDuration: TimeInterval = 1.0


enum AnimationType {
    case present
    case dismiss
}

class CNUpSlideAnimationTransition: NSObject {
    let animationType: AnimationType!
    
    init(animationType: AnimationType) {
        self.animationType = animationType
        super.init()
    }
}

extension CNUpSlideAnimationTransition: UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return transitonDuration
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        if animationType == .present {
            animationForPresent(using: transitionContext)
        } else {
            animationForDismiss(using: transitionContext)
        }
    }
    
    func animationForPresent(using transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView
        //1.Get fromVC and toVC
        guard let fromVC = transitionContext.viewController(forKey: .from) as? CNUpSlideViewController else { return }
        guard let toNaviVC = transitionContext.viewController(forKey: .to) as? CNSlideBaseNavigationController else { return }
        guard let selectedCell = fromVC.selectedCell else { return }
        let toVC = toNaviVC.topViewController as! CNUpSlideDetailViewController
        let frame = selectedCell.convert(selectedCell.bgBackView.frame, to: fromVC.view)
        //2.Set presentation original size.
        toVC.view.frame = frame
        toVC.scrollView.imageView.frame.size.width = GlobalConstants.todayCardSize.width
        toVC.scrollView.imageView.frame.size.height = GlobalConstants.todayCardSize.height
        
        containerView.addSubview(toNaviVC.view)
        
        //3.Change original size to final size with animation.
        UIView.animate(withDuration: transitonDuration, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
            toVC.view.frame = UIScreen.main.bounds
            toVC.scrollView.imageView.frame.size.width = KSreenWidth
            toVC.scrollView.imageView.frame.size.height = KSreenWidth*DownSlideSingleCourseImgH/DownSlideSingleCourseW
            toVC.closeBtn.alpha = 1
            
        }) { (completed) in
            transitionContext.completeTransition(completed)
        }
    }
    
    func animationForDismiss(using transitionContext: UIViewControllerContextTransitioning) {
        guard let fromNaviVC = transitionContext.viewController(forKey: .from) as? CNSlideBaseNavigationController else { return }
        guard let toVC = transitionContext.viewController(forKey: .to) as? CNUpSlideViewController else { return }
        guard let selectedCell = toVC.selectedCell else { return }
        let fromVC = fromNaviVC.topViewController as! CNUpSlideDetailViewController
        
        UIView.animate(withDuration: transitonDuration - 0.3, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
            let frame = selectedCell.convert(selectedCell.bgBackView.frame, to: toVC.view)
            fromVC.view.frame = frame
            fromVC.view.layer.cornerRadius = GlobalConstants.toDayCardCornerRadius
            fromVC.scrollView.imageView.frame.size.width = GlobalConstants.todayCardSize.width
            fromVC.scrollView.imageView.frame.size.height = GlobalConstants.todayCardSize.height
            fromVC.closeBtn.alpha = 0
            
        }) { (completed) in
            transitionContext.completeTransition(completed)
        }
    }
    
}