Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonjh1217/e0c51b7f4defade7702d2be1b567f927 to your computer and use it in GitHub Desktop.
Save sonjh1217/e0c51b7f4defade7702d2be1b567f927 to your computer and use it in GitHub Desktop.
UIViewControllerTransitioningDelegate
extension ChromecastExpandedViewController: UIViewControllerTransitioningDelegate {
public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return VerticalStackDismissAnimatedTransitioning()
}
}
import UIKit
class VerticalStackDismissAnimatedTransitioning: NSObject, UIViewControllerAnimatedTransitioning {
private let animatedTransitionDuration: TimeInterval = 0.4
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return animatedTransitionDuration
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromViewController = transitionContext.viewController(forKey: .from),
let toViewController = transitionContext.viewController(forKey: .to) else {
return
}
let originalFromViewControllerFrame = fromViewController.view.frame
let originalToViewControllerFrame = fromViewController.view.convert(toViewController.view.frame, from: toViewController.view)
let finalFromViewControllerFrame = CGRect(origin: CGPoint(x: originalToViewControllerFrame.minX, y: originalToViewControllerFrame.maxY), size: originalFromViewControllerFrame.size)
let initialToViewControllerFrameOriginY = originalFromViewControllerFrame.origin.y - originalToViewControllerFrame.size.height
let initialToViewControllerFrame = CGRect(origin: CGPoint(x: originalFromViewControllerFrame.origin.x, y: initialToViewControllerFrameOriginY), size: originalToViewControllerFrame.size)
guard let snapshottedToView = toViewController.view.snapshotView(afterScreenUpdates: true) else {
return
}
transitionContext.containerView.addSubview(snapshottedToView)
snapshottedToView.frame = initialToViewControllerFrame
snapshottedToView.alpha = 0
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
fromViewController.view.frame = finalFromViewControllerFrame
snapshottedToView.frame = originalToViewControllerFrame
snapshottedToView.alpha = 1
}) { (_) in
snapshottedToView.removeFromSuperview()
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment