title: How to animate Tab bar tab switch with a CrossDissolve slide transition?
body: <p>I'm trying to create a transition effect on a <code>UITabBarController</code> somewhat similar to the Facebook app. I managed to get a "scrolling effect" working on tab switch, but I can't seem to figure out how to cross dissolve (or it doesn't work at least).</p>
  
  <p>Here's my current code:</p>
  
  <pre><code>import UIKit
  
  class ScrollingTabBarControllerDelegate: NSObject, UITabBarControllerDelegate {
      func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -&gt; 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?) -&gt; 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 &lt; 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
          })
      }
  }
  </code></pre>
  
  <p>Would be great if anyone know how to get this to work, been trying for days now without progress... :/</p>
  
  <p><strong>edit:</strong> I got a cross dissolve working by replacing the UIView.animate block with: </p>
  
  <pre><code>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
  
  })
  </code></pre>
  
  <p><em>However</em>, the animation is really laggy and not usable :(</p>
  
  <p><strong>edit 2:</strong> For people trying to use these snippets, don't forget to hook up the delegate for the <code>UITabBarController</code>, otherwise nothing will happen.</p>
  
  <p><strong>edit 3:</strong> I've found a Swift library that does exactly what I was looking for:
  <a href="https://github.com/Interactive-Studio/TransitionableTab" rel="noreferrer">https://github.com/Interactive-Studio/TransitionableTab</a></p>