【问题标题】:iOS Custom animation for hidesBottomBarWhenPushedhidesBottomBarWhenPushed 的 iOS 自定义动画
【发布时间】:2018-05-29 05:53:28
【问题描述】:

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

问题是当循环动画出现时,底部的tabBar默认是向左滑动的。我想自定义那个动画来做一些不同的事情(可能是溶解或其他事情)。

这可能吗?

附言我会尽量避免通过设置 'isHidden=true' 或 'alpha=0' 来隐藏底部的 TabBar,因为它会增加一些小麻烦。

【问题讨论】:

    标签: ios swift uiviewcontroller uinavigationcontroller uitabbarcontroller


    【解决方案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()
        }
    }
    

    编辑:
    上述解决方案似乎不适用于横向。 我改为采用以下解决方案,但它变得越来越hackier。 不知何故,您只能在开始自己的动画后从 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
            }
        }
    }
    

    【讨论】:

    • 没有transitionContext设置为true,视图将如何显示如下? transitionContext.completeTransition(true)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 2016-04-30
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多