hideBottomBarWhenPushed的iOS自定义动画

问题描述 投票:7回答:1

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

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

这可能吗?

ps.s。我会尝试通过设置'isHidden = true'或'alpha = 0'来避免仅隐藏底部的TabBar,因为这会增加一些小麻烦。

ios swift uiviewcontroller uinavigationcontroller uitabbarcontroller
1个回答
0
投票

我今天遇到了这个问题,成功地使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()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.