Created
April 28, 2021 13:38
-
-
Save sonjh1217/e0c51b7f4defade7702d2be1b567f927 to your computer and use it in GitHub Desktop.
UIViewControllerTransitioningDelegate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ChromecastExpandedViewController: UIViewControllerTransitioningDelegate { | |
public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { | |
return VerticalStackDismissAnimatedTransitioning() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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