Tusker/Tusker/Screens/Large Image/Transitions/LargeImageShrinkAnimationCo...

73 lines
2.6 KiB
Swift
Raw Normal View History

2018-09-02 20:59:20 +00:00
//
// LargeImageShrinkAnimationController.swift
// Tusker
//
// Created by Shadowfacts on 9/1/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
2018-11-09 20:48:08 +00:00
import Gifu
2018-09-02 20:59:20 +00:00
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)
2018-11-09 20:48:08 +00:00
let imageView = GIFImageView(frame: originalFrame)
2018-09-02 20:59:20 +00:00
imageView.image = fromVC.image!
2018-11-09 20:48:08 +00:00
if let gifData = fromVC.gifData {
imageView.animate(withGIFData: gifData)
}
2018-09-02 20:59:20 +00:00
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = 0
2018-09-02 20:59:20 +00:00
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!
2018-09-02 20:59:20 +00:00
blackView.alpha = 0
}, completion: { _ in
blackView.removeFromSuperview()
imageView.removeFromSuperview()
if transitionContext.transitionWasCancelled {
toVC.view.removeFromSuperview()
}
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
}