73 lines
2.6 KiB
Swift
73 lines
2.6 KiB
Swift
//
|
|
// LargeImageShrinkAnimationController.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 9/1/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Gifu
|
|
|
|
class LargeImageShrinkAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
|
|
|
|
let interactionController: LargeImageInteractionController?
|
|
|
|
init(interactionController: LargeImageInteractionController?) {
|
|
self.interactionController = interactionController
|
|
}
|
|
|
|
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
|
|
return 0.2
|
|
}
|
|
|
|
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
|
|
guard let fromVC = transitionContext.viewController(forKey: .from) as? LargeImageViewController,
|
|
let toVC = transitionContext.viewController(forKey: .to),
|
|
let finalFrame = fromVC.originFrame else {
|
|
return
|
|
}
|
|
|
|
let originalVCFrame = fromVC.view.frame
|
|
|
|
let containerView = transitionContext.containerView
|
|
let image = fromVC.image!
|
|
let ratio = image.size.width / image.size.height
|
|
let width = originalVCFrame.width
|
|
let height = width / ratio
|
|
let originalFrame = CGRect(x: originalVCFrame.midX - width / 2, y: originalVCFrame.midY - height / 2, width: width, height: height)
|
|
|
|
let imageView = GIFImageView(frame: originalFrame)
|
|
imageView.image = fromVC.image!
|
|
if let gifData = fromVC.gifData {
|
|
imageView.animate(withGIFData: gifData)
|
|
}
|
|
imageView.contentMode = .scaleAspectFill
|
|
imageView.layer.cornerRadius = 0
|
|
imageView.layer.masksToBounds = true
|
|
|
|
let blackView = UIView(frame: originalVCFrame)
|
|
blackView.backgroundColor = .black
|
|
blackView.alpha = 1
|
|
|
|
containerView.addSubview(toVC.view)
|
|
containerView.addSubview(blackView)
|
|
containerView.addSubview(imageView)
|
|
|
|
let duration = transitionDuration(using: transitionContext)
|
|
UIView.animate(withDuration: duration, animations: {
|
|
imageView.frame = finalFrame
|
|
imageView.layer.cornerRadius = fromVC.originCornerRadius!
|
|
blackView.alpha = 0
|
|
}, completion: { _ in
|
|
blackView.removeFromSuperview()
|
|
imageView.removeFromSuperview()
|
|
if transitionContext.transitionWasCancelled {
|
|
toVC.view.removeFromSuperview()
|
|
}
|
|
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
|
|
})
|
|
}
|
|
|
|
}
|