Created
November 24, 2016 07:54
-
-
Save masamichiueta/91bd5c6f5f9292ce1870232b2fe90a4a 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
import UIKit | |
class ScaledModalAnimationController: NSObject, UIViewControllerAnimatedTransitioning { | |
let presenting: Bool | |
init(presenting: Bool) { | |
self.presenting = presenting | |
} | |
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { | |
return 0.35 | |
} | |
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { | |
if self.presenting { | |
present(using: transitionContext) | |
} else { | |
dismiss(using: transitionContext) | |
} | |
} | |
func present(using transitionContext: UIViewControllerContextTransitioning) { | |
guard let fromVC = transitionContext.viewController(forKey: .from), let toVC = transitionContext.viewController(forKey: .to | |
) else { | |
return | |
} | |
let containerView = transitionContext.containerView | |
let dimmingView = UIView(frame: fromVC.view.frame) | |
dimmingView.backgroundColor = UIColor.black | |
dimmingView.alpha = 0 | |
let finalFrame = transitionContext.finalFrame(for: toVC) | |
toVC.view.frame = CGRect(x: 0, y: finalFrame.height, width: finalFrame.width, height: finalFrame.height) | |
containerView.addSubview(dimmingView) | |
containerView.addSubview(toVC.view) | |
UIView.animate(withDuration: transitionDuration(using: transitionContext), | |
delay: 0, | |
options: [.curveEaseInOut], | |
animations: { | |
fromVC.view.transform = CGAffineTransform(scaleX: 0.95, y: 0.95) | |
dimmingView.alpha = 0.8 | |
toVC.view.frame = finalFrame | |
}, | |
completion: { finished in | |
dimmingView.removeFromSuperview() | |
fromVC.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0) | |
transitionContext.completeTransition(!transitionContext.transitionWasCancelled) | |
}) | |
} | |
func dismiss(using transitionContext: UIViewControllerContextTransitioning) { | |
guard let fromVC = transitionContext.viewController(forKey: .from), let toVC = transitionContext.viewController(forKey: .to | |
) else { | |
return | |
} | |
let containerView = transitionContext.containerView | |
let finalFrame = transitionContext.finalFrame(for: toVC) | |
toVC.view.transform = CGAffineTransform(scaleX: 0.95, y: 0.95) | |
let dimmingView = UIView(frame: fromVC.view.frame) | |
dimmingView.backgroundColor = UIColor.black | |
dimmingView.alpha = 0.8 | |
containerView.addSubview(toVC.view) | |
containerView.addSubview(dimmingView) | |
containerView.addSubview(fromVC.view) | |
UIView.animate(withDuration: transitionDuration(using: transitionContext), | |
delay: 0, | |
options: [.curveEaseInOut], | |
animations: { | |
toVC.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0) | |
dimmingView.alpha = 0.0 | |
fromVC.view.frame = CGRect(x: 0, y: finalFrame.height, width: finalFrame.width, height: finalFrame.height) | |
}, | |
completion: { finished in | |
dimmingView.removeFromSuperview() | |
fromVC.view.removeFromSuperview() | |
transitionContext.completeTransition(!transitionContext.transitionWasCancelled) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment