【问题标题】:Animate barTintColor on push segue动画 barTintColor on push segue
【发布时间】:2014-05-29 14:09:29
【问题描述】:

从一个view controller 转换到另一个时,我想为barTintColor 属性设置动画。

我尝试了以下解决方案:

[[self transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
} completion:nil];

它应该为 barTintColor 设置动画,但我的 导航栏 应该是半透明的。为了解决这个问题,我尝试在trasitionCoordinatoranimation 的完成块中将其设置为translucent = YES。像这样:

completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
    self.navigationController.navigationBar.translucent = YES;
}

但这会导致动画完成后导航栏barTintColor值略有跳跃。这是意料之中的,因为半透明和不透明的颜色不同。

类似的问题: [1] [2]

提前致谢!

编辑

如果我从动画块中删除 self.navigationController.navigationBar.translucent = NO; 行,我忘了补充说,导航栏的左上角会出现一些奇怪的缩放动画。所以这也不是解决方案。

【问题讨论】:

    标签: ios iphone objective-c uinavigationbar bartintcolor


    【解决方案1】:

    我将回答我自己的问题,同时仍对其他建议/解决方案持开放态度。

    我认为非常“干净”的解决方案是用适当的混合颜色从原始颜色制作 alpha 混合颜色。因此,假设我们在 导航栏 后面有一个白色背景,我们希望将当前 导航栏 barTintColor 设置为绿色。在这种情况下,我们必须将原始绿色与白色和适当的 alpha 值混合。

    在玩了一些值之后,我发现 alpha 0.16 最适合 iOS 7 案例(我还没有在其他操作系统版本上测试过)。

    用我的解决方案实现的原始问题中编写的动画块如下所示:

    注意ALPHA BLENDED ORIGINAL COLORORIGINAL COLOR

    [[self transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.barTintColor = <ALPHA BLENDED ORIGINAL COLOR>;
    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        self.navigationController.navigationBar.barTintColor = <ORIGINAL COLOR>;
        self.navigationController.navigationBar.translucent = YES;
    }];
    

    Alpha 混合方程

    CGFloat bR = ((bcR - cR) * alpha + cR);
    CGFloat bG = ((bcG - cG) * alpha + cG);
    CGFloat bB = ((bcB - cB) * alpha + cB);
    

    价值观

    cR, cG, cB     = original color R,G,B components
    bcR, bcG, bcB  = blending color R,G,B components
    alpha          = alpha value to blend with
    ================================================
    bR, bG, bB     = blended R,G,B components
    

    欢迎喜欢此解决方案的任何人使用我为此案例编写的 UIColor 的 alpha 混合类别。你可以找到它here

    谢谢。

    【讨论】:

    • 对我来说没什么区别。 iOS9/10 根本没有动画,只是一步改变颜色。您的解决方案是否仍然有效,或者您是否有在线应用可以让我检查是否有效?
    猜你喜欢
    • 2013-03-14
    • 1970-01-01
    • 2017-02-05
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多