Skip to content

Instantly share code, notes, and snippets.

@brunomunizaf
Created November 19, 2020 06:32
Show Gist options
  • Save brunomunizaf/93f469a7a277911060f2a7659b97557f to your computer and use it in GitHub Desktop.
Save brunomunizaf/93f469a7a277911060f2a7659b97557f to your computer and use it in GitHub Desktop.
import UIKit
final class Transition: NSObject, UIViewControllerAnimatedTransitioning {
enum Direction {
case present
case dismiss
}
var direction: Direction = .present
private var presentedConstraints: [NSLayoutConstraint] = []
private var dismissedConstraints: [NSLayoutConstraint] = []
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { 0.5 }
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
switch direction {
case .present:
present(using: transitionContext)
case .dismiss:
dismiss(using: transitionContext)
}
}
private func present(using context: UIViewControllerContextTransitioning) {
guard let presentedView = context.view(forKey: .to) else {
context.completeTransition(false)
return
}
context.containerView.addSubview(presentedView)
presentedView.translatesAutoresizingMaskIntoConstraints = false
presentedConstraints = [
presentedView.leftAnchor.constraint(equalTo: context.containerView.leftAnchor),
presentedView.rightAnchor.constraint(equalTo: context.containerView.rightAnchor),
presentedView.topAnchor.constraint(equalTo: context.containerView.topAnchor),
presentedView.bottomAnchor.constraint(equalTo: context.containerView.bottomAnchor)
]
dismissedConstraints = [
presentedView.leftAnchor.constraint(equalTo: context.containerView.leftAnchor),
presentedView.rightAnchor.constraint(equalTo: context.containerView.rightAnchor),
presentedView.topAnchor.constraint(equalTo: context.containerView.topAnchor),
presentedView.bottomAnchor.constraint(equalTo: context.containerView.centerYAnchor)
]
NSLayoutConstraint.activate(dismissedConstraints)
context.containerView.setNeedsLayout()
context.containerView.layoutIfNeeded()
NSLayoutConstraint.deactivate(dismissedConstraints)
NSLayoutConstraint.activate(presentedConstraints)
UIView.animate(
withDuration: transitionDuration(using: context),
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: .curveEaseInOut,
animations: {
context.containerView.setNeedsLayout()
context.containerView.layoutIfNeeded()
},
completion: { _ in
context.completeTransition(true)
})
}
private func dismiss(using context: UIViewControllerContextTransitioning) {
NSLayoutConstraint.deactivate(presentedConstraints)
NSLayoutConstraint.activate(dismissedConstraints)
UIView.animate(
withDuration: transitionDuration(using: context),
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: .curveEaseInOut,
animations: {
context.containerView.setNeedsLayout()
context.containerView.layoutIfNeeded()
},
completion: { _ in
context.completeTransition(true)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment