// // LargeImageExpandAnimationController.swift // Tusker // // Created by Shadowfacts on 9/1/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit class LargeImageExpandAnimationController: NSObject, UIViewControllerAnimatedTransitioning { func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { return 0.2 } func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { guard let fromVC = transitionContext.viewController(forKey: .from), let toVC = transitionContext.viewController(forKey: .to) as? LargeImageViewController, let originFrame = toVC.originFrame else { return } let containerView = transitionContext.containerView let finalVCFrame = transitionContext.finalFrame(for: toVC) let image = toVC.image! let ratio = image.size.width / image.size.height let width = finalVCFrame.width let height = width / ratio let finalFrame = CGRect(x: finalVCFrame.midX - width / 2, y: finalVCFrame.midY - height / 2, width: width, height: height) let imageView = UIImageView(frame: originFrame) imageView.image = toVC.image! imageView.contentMode = .scaleAspectFill imageView.layer.cornerRadius = toVC.originCornerRadius! imageView.layer.masksToBounds = true let blackView = UIView(frame: finalVCFrame) blackView.backgroundColor = .black blackView.alpha = 0 containerView.addSubview(toVC.view) containerView.addSubview(blackView) containerView.addSubview(imageView) toVC.view.isHidden = true let duration = transitionDuration(using: transitionContext) UIView.animate(withDuration: duration, animations: { imageView.frame = finalFrame imageView.layer.cornerRadius = 0 blackView.alpha = 1 }, completion: { _ in toVC.view.isHidden = false fromVC.view.isHidden = false blackView.removeFromSuperview() imageView.removeFromSuperview() transitionContext.completeTransition(!transitionContext.transitionWasCancelled) }) } }