Skip to content

Instantly share code, notes, and snippets.

@TiagoBras
Created April 18, 2019 11:37
Show Gist options
  • Select an option

  • Save TiagoBras/41ddeea89dcf43dbabee4d253fde25bf to your computer and use it in GitHub Desktop.

Select an option

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
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