Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steryokhin/f136bd3e0c38b0fcdbff30b311714257 to your computer and use it in GitHub Desktop.
Save steryokhin/f136bd3e0c38b0fcdbff30b311714257 to your computer and use it in GitHub Desktop.
Simple sliding transitioning for navigation controller mimics pre iOS7 push/pop animations.All you need, is simply set delegate for you navigation controller: self.navigationController.delegate = self.navigationTransitioningDelegate;NavigationController does not retain delegate, so you should hold it. Note: this code is free. Ported from https:/…
import Foundation
import UIKit
class ClassicalNavigationAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
let DEFAULT_TRANSITION_DURATION: TimeInterval = 0.3
var reverse: Bool = false
init(withReverse reverse: Bool) {
self.reverse = reverse
super.init()
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return DEFAULT_TRANSITION_DURATION
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromController = transitionContext.viewController(forKey: .from),
let toController = transitionContext.viewController(forKey: .to) else
{
return
}
let containerView = transitionContext.containerView
let duration: TimeInterval = self.transitionDuration(using: transitionContext)
let viewWidth = containerView.frame.width
var fromViewFrame = fromController.view.frame
var toViewFrame = toController.view.frame
toViewFrame.origin.x = self.reverse ? -viewWidth : viewWidth
toController.view.frame = toViewFrame
containerView.addSubview(toController.view)
UIView.animate(withDuration: duration, delay: 0.0, options: .curveEaseInOut, animations: {
toViewFrame.origin.x = containerView.frame.minX
fromViewFrame.origin.x = self.reverse ? viewWidth : -viewWidth
toController.view.frame = toViewFrame
fromController.view.frame = fromViewFrame
}) { (finished) in
if self.reverse {
fromController.view.removeFromSuperview()
}
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
}
import Foundation
import UIKit
/**
* Idea of this class came after reading this thread:
* http://stackoverflow.com/questions/18867248/restore-pre-ios7-uinavigationcontroller-pushviewcontroller-animation
* and this gist: https://gist.github.com/ArtFeel/7690431
* It implements Simple sliding transitioning for navigation controller mimics pre iOS7 push/pop animations.
* All you need, is simply set delegate for you navigation controller.
*/
class ClassicalTransitionDelegate : NSObject, UINavigationControllerDelegate {
var pushTransactioning: UIViewControllerAnimatedTransitioning!
var popTransactioning: UIViewControllerAnimatedTransitioning!
override init() {
self.pushTransactioning = ClassicalNavigationAnimationController(withReverse: false)
self.popTransactioning = ClassicalNavigationAnimationController(withReverse: true)
super.init()
}
func navigationController(_ navigationController: UINavigationController,
animationControllerFor operation: UINavigationControllerOperation,
from fromVC: UIViewController,
to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
return operation == .pop ? self.popTransactioning : self.pushTransactioning
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment