Created
July 3, 2017 18:19
-
-
Save sam-moshenko/2bc1461b63af2c137db83ce4ee068707 to your computer and use it in GitHub Desktop.
Simple class for fading animation between UIViewControllers in UINavigationController
This file contains 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
// | |
// 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