Created
April 18, 2019 11:37
-
-
Save TiagoBras/41ddeea89dcf43dbabee4d253fde25bf to your computer and use it in GitHub Desktop.
Animated transition that makes the view controller slide in from the bottom and dims the background
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 ActionSheetAnimationController: NSObject, UIViewControllerAnimatedTransitioning { | |
| enum Direction { | |
| case enter, exit | |
| } | |
| let direction: Direction | |
| let duration: TimeInterval | |
| init(direction: Direction, duration: TimeInterval = 0.3) { | |
| self.direction = direction | |
| self.duration = duration | |
| } | |
| func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { | |
| return duration | |
| } | |
| func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { | |
| guard let fromVC = transitionContext.viewController(forKey: .from), | |
| let toVC = transitionContext.viewController(forKey: .to) else { | |
| transitionContext.completeTransition(true) | |
| return | |
| } | |
| if direction == .enter { | |
| transitionContext.containerView.addSubview(toVC.view) | |
| transitionContext.containerView.backgroundColor = UIColor.clear | |
| toVC.view.frame.origin.y = toVC.view.frame.height | |
| } | |
| let animations = { | |
| if self.direction == .enter { | |
| transitionContext.containerView.backgroundColor = UIColor.black.withAlphaComponent(0.4) | |
| toVC.view.frame.origin.y = 0 | |
| } else { | |
| transitionContext.containerView.backgroundColor = UIColor.clear | |
| fromVC.view.frame.origin.y = fromVC.view.frame.height | |
| } | |
| } | |
| UIView.animate( | |
| withDuration: transitionDuration(using: transitionContext), | |
| delay: 0, | |
| options: [.curveEaseInOut], | |
| animations: animations) { (_) in | |
| transitionContext.completeTransition(true) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment