【问题标题】:Animate navigation bar color in custom transition自定义过渡中的动画导航栏颜色
【发布时间】:2019-12-21 14:23:30
【问题描述】:

我正在为模态演示文稿使用自定义过渡 (iOS 12-13)。
我有暗模式的问题。
第一个黑色背景的控制器不可见。

如何用动画改变导航栏颜色?

就像在«提醒»(iOS 13)中:

【问题讨论】:

  • 在显示视图控制器之前,将导航的 tintColor 更改为灰色,并在关闭完成后将其更改回黑色。

标签: swift uinavigationcontroller uinavigationbar


【解决方案1】:

我解决了我的问题。 我将只为 iOS 12 使用自定义过渡。

我的部分动画代码(用于演示):

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let container       = transitionContext.containerView
        let fromController  = transitionContext.viewController(forKey: .from)!
        let toController    = transitionContext.viewController(forKey: .to)!
        let originalFrame   = transitionContext.finalFrame(for: toController)
        let toView          = transitionContext.view(forKey: .to)!

        let fromView = transitionContext.viewController(forKey: .from)!

        container.addSubview(toView)

        let offset = SlideTransitionHelper.offset(controllerSize: originalFrame.size)
        toView.frame = originalFrame.offsetBy(dx: offset.x, dy: offset.y)
        let scale: CGFloat = UIApplication.shared.statusBarFrame.height == 20 ? 0.93 : 0.91

        UIView.animate(
            withDuration: self.transitionDuration(using: transitionContext),
            delay: 0,
            usingSpringWithDamping: 1,
            initialSpringVelocity: 0.4,
            options: .allowUserInteraction,
            animations: {
                toView.frame = originalFrame
                toView.roundCorners([.topLeft, .topRight], radius: 12)
                toView.layer.masksToBounds = true

                if let tabBarController = fromController as? UITabBarController {
                    if let activeController = tabBarController.selectedViewController as? UINavigationController, let childController = activeController.children.first{

                        //Animate NavigationBar color
                        childController.navigationController?.navigationBar.barTintColor = self.themeManager.current.systemSecondaryColor
                        childController.navigationController?.navigationBar.layoutIfNeeded()

                        //Animate controller background color
                        childController.view.backgroundColor = self.themeManager.current.systemSecondaryColor

                    }
                }

                //Scale controller
                var transform = CATransform3DIdentity
                transform = CATransform3DScale(transform, scale, scale, 1.0)
                transform = CATransform3DTranslate(transform, 0, scale, 0)
                fromView.view.layer.transform = transform
                fromView.view.layer.cornerRadius = 12
                fromView.view.layer.masksToBounds = true
        },
            completion: { (isFinished) in
            transitionContext.completeTransition(isFinished)
        })
    }

【讨论】:

    猜你喜欢
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 2013-12-21
    • 1970-01-01
    • 2012-09-17
    • 2017-07-08
    • 1970-01-01
    相关资源
    最近更新 更多