0

Ok, having some Tab bar controller animation problems trying to mimic the animation transition in Apple's iTunes when they leave the music player VC. Right now I have a media bar which is just a UIView that sits on the bottom of the screen and animates up whenever I select a tab that isn't the music player view (so selectedIndex != (viewControllers?.count)!-1)

This is fine, but even looking at other tab bar animations I cant make something like the vertical/squish animation transition from iTunes. This answer gave me how to do it horizontally like a page - iPhone: How to switch tabs with an animation?

However it doesn't work when you make it vertical. I basically want the dismiss transition as if the tab were a modal view controller. What I have:

func animateToTab(toIndex: Int) {
        let tabViewControllers = viewControllers!
        let fromView = selectedViewController!.view
        let toView = tabViewControllers[toIndex].view
        let fromIndex = tabViewControllers.index(of: selectedViewController!)

        guard fromIndex != toIndex else {return}

        // Add the toView to the tab bar view
        fromView?.superview!.addSubview(toView!)
        fromView?.superview!.sendSubview(toBack: toView!)


        // Disable interaction during animation
        view.isUserInteractionEnabled = false

        UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 0.3, options: UIViewAnimationOptions.curveEaseOut, animations: {

            // Slide the views by -offset
            fromView?.superview!.center.y += screenSize.height
            //toView?.center   = CGPoint(x: (toView?.center.x)! - offset, y: (toView?.center.y)!);

        }, completion: { finished in

            // Remove the old view from the tabbar view.
            fromView?.removeFromSuperview()
            self.view.isUserInteractionEnabled = true
        })
        self.selectedIndex = toIndex
    }

But the current VC, the music player, snaps back to its original position after it moves and switches the selectedIndex. Its like the tab bar controller makes all its viewcontrollers be in the right position before switching.

How can I mimic a modal dismiss animation with a tab bar VC?

blue
  • 7,175
  • 16
  • 81
  • 179
  • If you want custom transition as you `present` a view controller, set it's `transitioningDelegate` and do a custom animation. Then you can just `present` and `dismiss` like normal, but just with you custom animation. See [Custom Transitions using View Controllers](https://developer.apple.com/videos/play/wwdc2013/218/) (and the latter part of [A Look Inside Presentation Controllers](https://developer.apple.com/videos/play/wwdc2014/228/) for additional details on presentation controllers used for add'l chrome). – Rob Aug 20 '17 at 20:53
  • If you want custom animation as you transition between tabs in your tab view controller, you again use animation controller described in [Custom Transitions using View Controllers](https://developer.apple.com/videos/play/wwdc2013/218/), but vend the transitioning delegate by specifying the `delegate` of the tab bar controller and implement [`animationControllerForTransitionFrom`](https://developer.apple.com/documentation/uikit/uitabbarcontrollerdelegate/1621167-tabbarcontroller). But I assume you want `present`/`dismiss` outlined in previous comment. – Rob Aug 20 '17 at 20:56
  • @Rob Thanks Rob, looking at the video now but could you provide a code example of how to use this in my case? That way I could accept your answer – blue Aug 20 '17 at 21:02
  • For animated `present`/`dismiss`, see https://stackoverflow.com/a/45764323/1271826. That's popping out from the side, but you can presumably see how it could be tailored to pop in from the bottom, too. I have to warn you that this custom transitioning of view controllers is a little confusing when you first start playing around with it (we're vending all sorts of little objects conforming to confusingly named protocols), but once you grok it, you'll see it's a pretty powerful pattern. – Rob Aug 20 '17 at 21:05

0 Answers0