Skip to content

Instantly share code, notes, and snippets.

@wata
Created February 27, 2020 08:46
Show Gist options
  • Save wata/85b68ba9b2b3564f8dfdd2af6568a359 to your computer and use it in GitHub Desktop.
Save wata/85b68ba9b2b3564f8dfdd2af6568a359 to your computer and use it in GitHub Desktop.
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