I am trying to create a transition effect on UITabBarController with something similar to a Facebook application. I managed to get a “scroll effect” that works on a tab switch, but I can't figure out how cross dissolving (or at least it doesn't work).
Here is my current code:
import UIKit class ScrollingTabBarControllerDelegate: NSObject, UITabBarControllerDelegate { func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { return ScrollingTransitionAnimator(tabBarController: tabBarController, lastIndex: tabBarController.selectedIndex) } } class ScrollingTransitionAnimator: NSObject, UIViewControllerAnimatedTransitioning { weak var transitionContext: UIViewControllerContextTransitioning? var tabBarController: UITabBarController! var lastIndex = 0 func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { return 0.2 } init(tabBarController: UITabBarController, lastIndex: Int) { self.tabBarController = tabBarController self.lastIndex = lastIndex } func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { self.transitionContext = transitionContext let containerView = transitionContext.containerView let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) containerView.addSubview(toViewController!.view) var viewWidth = toViewController!.view.bounds.width if tabBarController.selectedIndex < lastIndex { viewWidth = -viewWidth } toViewController!.view.transform = CGAffineTransform(translationX: viewWidth, y: 0) UIView.animate(withDuration: self.transitionDuration(using: (self.transitionContext)), delay: 0.0, usingSpringWithDamping: 1.2, initialSpringVelocity: 2.5, options: .transitionCrossDissolve, animations: { toViewController!.view.transform = CGAffineTransform.identity fromViewController!.view.transform = CGAffineTransform(translationX: -viewWidth, y: 0) }, completion: { _ in self.transitionContext?.completeTransition(!self.transitionContext!.transitionWasCancelled) fromViewController!.view.transform = CGAffineTransform.identity }) } }
It would be great if someone knew how to get this to work, trying for many days without progress ...: /
edit: I got the cross-dissolution work by replacing the UIView.animate block with:
UIView.transition(with: containerView, duration: 0.2, options: .transitionCrossDissolve, animations: { toViewController!.view.transform = CGAffineTransform.identity fromViewController!.view.transform = CGAffineTransform(translationX: -viewWidth, y: 0) }, completion: { _ in self.transitionContext?.completeTransition(!self.transitionContext!.transitionWasCancelled) fromViewController!.view.transform = CGAffineTransform.identity })
However, the animation is really late and not used :(
edit 2: For people trying to use these snippets, be sure to include the delegate for the UITabBarController , otherwise nothing will happen.
edit 3: I found the Swift library that does exactly what I was looking for: https://github.com/Interactive-Studio/TransitionableTab