Created
February 27, 2020 08:46
-
-
Save wata/85b68ba9b2b3564f8dfdd2af6568a359 to your computer and use it in GitHub Desktop.
This file contains 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
final class FadeAnimator: NSObject, UIViewControllerAnimatedTransitioning { | |
private let presenting: Bool | |
init(presenting: Bool) { | |
self.presenting = presenting | |
} | |
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { | |
return 0.3 | |
} | |
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { | |
guard let fromView = transitionContext.view(forKey: .from) else { return } | |
guard let toView = transitionContext.view(forKey: .to) else { return } | |
let container = transitionContext.containerView | |
if presenting { | |
container.addSubview(toView) | |
toView.alpha = 0.0 | |
} else { | |
container.insertSubview(toView, belowSubview: fromView) | |
} | |
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: { | |
if self.presenting { | |
toView.alpha = 1.0 | |
} else { | |
fromView.alpha = 0.0 | |
} | |
}, completion: { (_) in | |
let success = !transitionContext.transitionWasCancelled | |
if !success { | |
toView.removeFromSuperview() | |
} | |
transitionContext.completeTransition(success) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment