Skip to content

Instantly share code, notes, and snippets.

@sveinhal
Created June 24, 2015 08:46
Show Gist options
  • Save sveinhal/71466380db975273d3e0 to your computer and use it in GitHub Desktop.
Save sveinhal/71466380db975273d3e0 to your computer and use it in GitHub Desktop.
import UIKit
class SlideOutTransitioning: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
private var presenting: Bool = false
private let duration = 0.3
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.presenting = true
return self
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.presenting = false
return self
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
return duration
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
if presenting {
present(transitionContext)
} else {
dismiss(transitionContext)
}
}
private func animate(animations: ()->(), completion: (Bool)->()) {
UIView.animateWithDuration(duration,
delay: 0.0,
usingSpringWithDamping: 0.8,
initialSpringVelocity: 0.0,
options: UIViewAnimationOptions.BeginFromCurrentState,
animations: animations,
completion: completion)
}
private func present(transitionContext: UIViewControllerContextTransitioning) {
let to = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
let from = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
let preferredWidth = to.preferredContentSize.width
let maxWidth = from.view.frame.width
let width = preferredWidth == 0 ? maxWidth : min(preferredWidth, maxWidth)
let (endFrame, _) = from.view.frame.rectsByDividing(width, fromEdge: .MaxXEdge)
to.view.frame = endFrame
to.view.transform = CGAffineTransformMakeTranslation(width, 0)
from.view.userInteractionEnabled = false
transitionContext.containerView().addSubview(to.view)
animate({
from.view.tintAdjustmentMode = .Dimmed
to.view.transform = CGAffineTransformIdentity
}, completion: { completed in
transitionContext.completeTransition(completed)
})
}
private func dismiss(transitionContext: UIViewControllerContextTransitioning) {
let to = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
let from = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
let width = from.view.frame.width
transitionContext.containerView().addSubview(from.view)
to.view.userInteractionEnabled = true
animate({
to.view.tintAdjustmentMode = .Automatic
from.view.transform = CGAffineTransformMakeTranslation(width, 0)
}, completion: { completed in
transitionContext.completeTransition(completed)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment