84 lines
3.0 KiB
Swift
84 lines
3.0 KiB
Swift
// GalleryExpandAnimationController.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 6/14/19.
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Gifu
|
|
|
|
class GalleryExpandAnimationController: 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? GalleryViewController else {
|
|
return
|
|
}
|
|
|
|
let finalVCFrame = transitionContext.finalFrame(for: toVC)
|
|
guard let (image, sourceFrame, sourceCornerRadius) = toVC.sourcesInfo[toVC.startIndex] else {
|
|
toVC.view.frame = finalVCFrame
|
|
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
|
|
return
|
|
}
|
|
|
|
let attachment = toVC.attachments[toVC.startIndex]
|
|
|
|
let containerView = transitionContext.containerView
|
|
|
|
let ratio = image.size.width / image.size.height
|
|
var width = finalVCFrame.width
|
|
var height = width / ratio
|
|
let maxHeight = fromVC.view.bounds.height - fromVC.view.safeAreaInsets.top - fromVC.view.safeAreaInsets.bottom
|
|
if height > maxHeight {
|
|
let scaleFactor = maxHeight / height
|
|
width *= scaleFactor
|
|
height = maxHeight
|
|
}
|
|
let finalFrame = CGRect(x: finalVCFrame.midX - width / 2, y: finalVCFrame.midY - height / 2, width: width, height: height)
|
|
|
|
let imageView = GIFImageView(frame: sourceFrame)
|
|
imageView.image = image
|
|
if attachment.url.pathExtension == "gif",
|
|
let data = ImageCache.attachments.get(attachment.url) {
|
|
imageView.animate(withGIFData: data)
|
|
}
|
|
imageView.contentMode = .scaleAspectFill
|
|
imageView.layer.cornerRadius = sourceCornerRadius
|
|
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.frame = finalVCFrame
|
|
|
|
toVC.view.isHidden = false
|
|
fromVC.view.isHidden = false
|
|
blackView.removeFromSuperview()
|
|
imageView.removeFromSuperview()
|
|
|
|
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
|
|
})
|
|
|
|
}
|
|
|
|
}
|