iOS的自定义动画hidesBottomBarWhenPushed

时间:2017-12-15 03:08:01

标签: ios swift uiviewcontroller uinavigationcontroller uitabbarcontroller

我的应用程序的结构是TabBarController - > NavigationController - > FirstViewController - > SecondViewController。我使用从FirstViewController到SecondViewController的自定义推送转换来模仿循环过渡。我不想在SecondViewController上显示底部的TabBar。所以我在SecondViewController上设置'hidesBottomBarWhenPushed = true'。

问题是当圆形动画发生时,默认情况下底部的tabBar会向左滑动。我想自定义该动画以做一些不同的事情(可能是溶解或其他事情)。

这可能吗?

P.S。我试图通过设置'isHidden = true'或'alpha = 0'来避免隐藏底部TabBar,因为它会增加一些小的并发症。

1 个答案:

答案 0 :(得分:1)

我今天遇到了这个问题,并成功地使TabBar向下滑动而不是向左滑动。至少可以在iOS 13上运行。

我使用动画器进行过渡,以使TabBar动画化并抵消默认动画。

class CustomTransition: NSObject, UIViewControllerAnimatedTransitioning {
    static let duration: TimeInterval = 0.5

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        Self.duration
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let from = transitionContext.viewController(forKey: .from)!
        let to = transitionContext.viewController(forKey: .to)!

        let animator = UIViewPropertyAnimator(duration: Self.duration, curve: .easeInOut)

        // Configure animator for transition like normal.
        // ...

        // Now handle the TabBar.
        if
            to.hidesBottomBarWhenPushed,
            !from.hidesBottomBarWhenPushed,
            let tabBar = from.tabBarController?.tabBar
        {
            // TabBar is going away.

            animator.addAnimations {
                // Counteract default animation by animating x in opposite direction.
                tabBar.center.x += tabBar.bounds.width

                // Animate TabBar down.
                tabBar.center.y += tabBar.bounds.height

                // Or alternatively animate opacity.
                // tabBar.alpha = 0
            }
        }
        else if
            !to.hidesBottomBarWhenPushed,
            from.hidesBottomBarWhenPushed,
            let tabBar = to.tabBarController?.tabBar
        {
            // TabBar is coming back.

            // TabBar by default will be animated toward default position.
            // Make sure it's already there on x so default animation does nothing for x.
            tabBar.center.x = tabBar.bounds.width / 2

            // Move y down, so default animation will move TabBar up to default position.
            tabBar.center.y += tabBar.bounds.height

            // Or alternatively animate opacity.
            // tabBar.alpha = 0
            // animator.addAnimations {
            //    tabBar.alpha = 1
            //}
        }

        animator.startAnimation()
    }
}

编辑:
上述解决方案似乎不适用于横向。 我选择了以下解决方案,但它变得越来越骇人听闻了。 以某种方式,您只能在启动自己的动画后从TabBar图层中删除默认动画,就像以某种方式相关。

我已经在不同方向上的各种设备上对此进行了测试,并且至少在iOS 13上,它似乎始终如一。

// Start transition animation.
animator.startAnimation()

if let tabBar = pushedController.tabBarController?.tabBar {
    // Remove default TabBar animation.
    tabBar.layer.removeAllAnimations()
    if pushedController == to {
        // Move TabBar back to its place.
        tabBar.center.x = tabBar.bounds.width / 2

        // Now animate it out again.
        animator.addAnimations {
            tabBar.center.y += tabBar.bounds.height
        }
    } else {
        // Move TabBar down out of view.
        tabBar.center.y += tabBar.bounds.height

        // Now animate it in.
        animator.addAnimations {
            tabBar.center.y -= tabBar.bounds.height
        }
    }
}