Skip to content

Instantly share code, notes, and snippets.

@sam-moshenko
Created July 3, 2017 18:19
Show Gist options
  • Save sam-moshenko/2bc1461b63af2c137db83ce4ee068707 to your computer and use it in GitHub Desktop.
Save sam-moshenko/2bc1461b63af2c137db83ce4ee068707 to your computer and use it in GitHub Desktop.
Simple class for fading animation between UIViewControllers in UINavigationController
//
// NavigationVC.swift
// WorkUp
//
// Created by Simon on 4/23/17.
// Copyright © 2017 Simon. All rights reserved.
//
import UIKit
//Usage example
// class NavigationVC: UINavigationController, UINavigationControllerDelegate {
// func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
// return TMFadeAnimator()
// }
// }
//The VC fading animator
class TMFadeAnimator: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let toViewController = transitionContext.viewController(forKey: .to)
let fromViewController = transitionContext.viewController(forKey: .from)
toViewController?.view.frame = transitionContext.containerView.frame
toViewController?.beginAppearanceTransition(true, animated: true)
fromViewController?.beginAppearanceTransition(false, animated: true)
transitionContext.containerView.addSubview(toViewController!.view)
toViewController!.view.alpha = 0.0
UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
toViewController!.view.alpha = 1.0
fromViewController!.view.alpha = 0.0
}) { (finished) in
toViewController?.endAppearanceTransition()
fromViewController?.endAppearanceTransition()
transitionContext.completeTransition(finished)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment