Tusker/Tusker/Screens/Large Image/Transitions/LargeImageExpandAnimationCo...

80 lines
3.1 KiB
Swift
Raw Normal View History

2018-09-02 20:59:20 +00:00
//
// LargeImageExpandAnimationController.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 LargeImageExpandAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.2
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromVC = transitionContext.viewController(forKey: .from),
2019-06-17 02:39:46 +00:00
let toVC = transitionContext.viewController(forKey: .to) as? LargeImageViewController else {
2018-09-02 20:59:20 +00:00
return
}
let containerView = transitionContext.containerView
containerView.addSubview(toVC.view)
2018-09-02 20:59:20 +00:00
let finalVCFrame = transitionContext.finalFrame(for: toVC)
guard let sourceInfo = toVC.sourceInfo,
let image = sourceInfo.image else {
toVC.view.frame = finalVCFrame
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
return
2019-06-17 02:39:46 +00:00
}
2018-09-02 20:59:20 +00:00
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 = GIFImageView(frame: sourceInfo.frame)
imageView.image = image
2018-11-09 20:48:08 +00:00
if let gifData = toVC.gifData {
imageView.animate(withGIFData: gifData)
}
2018-09-02 20:59:20 +00:00
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = sourceInfo.cornerRadius
2018-09-02 20:59:20 +00:00
imageView.layer.masksToBounds = true
let blackView = UIView(frame: finalVCFrame)
blackView.backgroundColor = .black
blackView.alpha = 0
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
2018-09-02 20:59:20 +00:00
blackView.alpha = 1
}, completion: { _ in
// This shouldn't be necessary. I believe it's a workaround for using a XIB
// for the large image VC. Without this, the final frame of the large image VC
// is not set to the propper rect (it uses the frame of the preview device
// in the XIB). When using a storyboard, the final frame is automatically set
// (or UIKit does layout differently when loading the view) and this is not necessary.
toVC.view.frame = finalVCFrame
2018-09-02 20:59:20 +00:00
toVC.view.isHidden = false
fromVC.view.isHidden = false
blackView.removeFromSuperview()
imageView.removeFromSuperview()
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
}