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
|
|
|
|
|
|
|
|
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) {
|
2020-03-18 01:05:45 +00:00
|
|
|
guard let fromVC = transitionContext.viewController(forKey: .from) as? LargeImageAnimatableViewController,
|
2019-06-17 02:39:46 +00:00
|
|
|
let toVC = transitionContext.viewController(forKey: .to) else {
|
2018-09-02 20:59:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-06 18:47:45 +00:00
|
|
|
if UIAccessibility.prefersCrossFadeTransitions && !transitionContext.isInteractive {
|
2020-09-12 17:25:23 +00:00
|
|
|
animateCrossFadeTransition(using: transitionContext)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-26 03:10:48 +00:00
|
|
|
guard let sourceView = fromVC.animationSourceView,
|
|
|
|
let sourceFrame = fromVC.sourceViewFrame(in: toVC.view),
|
2020-03-18 01:05:45 +00:00
|
|
|
let image = fromVC.animationImage else {
|
2020-01-26 03:29:12 +00:00
|
|
|
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
|
|
|
|
return
|
2019-06-17 02:39:46 +00:00
|
|
|
}
|
2020-01-26 03:29:12 +00:00
|
|
|
|
2020-03-26 03:10:48 +00:00
|
|
|
// use alpha, becaus isHidden makes stack views re-layout
|
|
|
|
sourceView.alpha = 0
|
2020-03-26 02:09:00 +00:00
|
|
|
|
2018-09-02 20:59:20 +00:00
|
|
|
let containerView = transitionContext.containerView
|
|
|
|
|
2020-03-26 02:09:00 +00:00
|
|
|
let originalVCFrame = fromVC.view.frame
|
|
|
|
var originalFrameSize = originalVCFrame.inset(by: fromVC.view.safeAreaInsets).size
|
|
|
|
let newWidth = originalFrameSize.width / image.size.width
|
|
|
|
let newHeight = originalFrameSize.height / image.size.height
|
|
|
|
if newHeight < newWidth {
|
|
|
|
originalFrameSize.width = newHeight * image.size.width
|
|
|
|
} else {
|
|
|
|
originalFrameSize.height = newWidth * image.size.height
|
|
|
|
}
|
|
|
|
let originalFrame = CGRect(origin: CGPoint(x: originalVCFrame.midX - originalFrameSize.width / 2, y: originalVCFrame.midY - originalFrameSize.height / 2), size: originalFrameSize)
|
|
|
|
|
2018-11-09 20:48:08 +00:00
|
|
|
let imageView = GIFImageView(frame: originalFrame)
|
2019-09-10 16:25:50 +00:00
|
|
|
imageView.image = image
|
2020-03-18 01:05:45 +00:00
|
|
|
if let gifData = fromVC.animationGifData {
|
2018-11-09 20:48:08 +00:00
|
|
|
imageView.animate(withGIFData: gifData)
|
|
|
|
}
|
2018-09-02 20:59:20 +00:00
|
|
|
imageView.contentMode = .scaleAspectFill
|
2018-09-02 22:22:29 +00:00
|
|
|
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
|
|
|
|
|
2021-09-22 03:30:38 +00:00
|
|
|
// Save the old superview of toVC so we can move it back after the animation.
|
|
|
|
// It would be better to just not move toVC and instead remove from fromVC from the
|
|
|
|
// containerView so that toVC is visible behind it, but that doens't work for some reason
|
|
|
|
// (the entire screen just goes black, not even the contents of the containerView appear).
|
|
|
|
let oldSuperview = toVC.view.superview
|
|
|
|
|
2018-09-02 20:59:20 +00:00
|
|
|
containerView.addSubview(toVC.view)
|
|
|
|
containerView.addSubview(blackView)
|
|
|
|
containerView.addSubview(imageView)
|
|
|
|
|
|
|
|
let duration = transitionDuration(using: transitionContext)
|
|
|
|
UIView.animate(withDuration: duration, animations: {
|
2020-03-26 03:10:48 +00:00
|
|
|
imageView.frame = sourceFrame
|
|
|
|
imageView.layer.cornerRadius = sourceView.layer.cornerRadius
|
|
|
|
imageView.layer.maskedCorners = sourceView.layer.maskedCorners
|
2018-09-02 20:59:20 +00:00
|
|
|
blackView.alpha = 0
|
|
|
|
}, completion: { _ in
|
|
|
|
blackView.removeFromSuperview()
|
|
|
|
imageView.removeFromSuperview()
|
|
|
|
if transitionContext.transitionWasCancelled {
|
|
|
|
toVC.view.removeFromSuperview()
|
|
|
|
}
|
2020-03-26 03:10:48 +00:00
|
|
|
|
|
|
|
sourceView.alpha = 1
|
|
|
|
|
2018-09-02 20:59:20 +00:00
|
|
|
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
|
2021-09-22 03:30:38 +00:00
|
|
|
|
|
|
|
// move the toVC back to the view that it was in before
|
|
|
|
oldSuperview?.addSubview(toVC.view)
|
2018-09-02 20:59:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:25:23 +00:00
|
|
|
func animateCrossFadeTransition(using transitionContext: UIViewControllerContextTransitioning) {
|
|
|
|
guard let fromVC = transitionContext.viewController(forKey: .from) as? LargeImageAnimatableViewController,
|
|
|
|
let toVC = transitionContext.viewController(forKey: .to) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
transitionContext.containerView.addSubview(toVC.view)
|
|
|
|
transitionContext.containerView.addSubview(fromVC.view)
|
|
|
|
|
|
|
|
let duration = transitionDuration(using: transitionContext)
|
|
|
|
UIView.animate(withDuration: duration) {
|
|
|
|
fromVC.view.alpha = 0
|
|
|
|
} completion: { (_) in
|
|
|
|
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-02 20:59:20 +00:00
|
|
|
}
|