过渡色 [英] Transition color

查看:160
本文介绍了过渡色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变我的导航栏的颜色,动画。

I would like to change the color of my navigation bar, with animation.

我试过,但CATransition我无法找到如何与改变颜色。

I tried CATransition but I can't find how to change the color with that.

有没有办法更改与衰落或任何其他动画的颜色?
 像TransitionDrawable在Android中。

Is there a way to change the color with a fading or any other animation ? Like TransitionDrawable in Android.

感谢。

推荐答案

我尝试了一些东西,并最终结束了这个解决方案。它不需要任何额外的层,图像或过渡视图和利用了内置在酒吧的动画。

I tried some stuff and finally ended up with this solution. It doesn't need any extra layers, images or transition views and makes use of the built-in animation of the bar.

因此​​,它不隐藏你放置你的导航栏上的标题和栏按钮。如果导航栏下方是状态栏,状态栏的颜色也随之发生变化。

Therefore it does not hide any titles and bar buttons that you have placed on your navigation bar. If the nav bar is below a status bar, the status bar's color will also be changed.

这与iOS7测试,无论是模拟器以及真实设备(新iPad)

This is tested with iOS7, both for simulator as well as real device (iPad)

的code:

右键下方的进口做

#define STEP_DURATION 0.01

您可以用这个值玩了平滑的动画

You can play with this value for a smoother animation

这是要调用的方法

[self animateNavigationBarFromColor:[UIColor greenColor] toColor:[UIColor blueColor] duration:2.5];

参数应该是自我解释(持续时间以秒为单位)。

Parameters should be self-explaining (duration is in seconds).

这是实际code

- (void)animateNavigationBarFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor duration:(NSTimeInterval)duration
{
    NSUInteger steps = duration / STEP_DURATION;

    CGFloat fromRed;
    CGFloat fromGreen;
    CGFloat fromBlue;
    CGFloat fromAlpha;

    [fromColor getRed:&fromRed green:&fromGreen blue:&fromBlue alpha:&fromAlpha];

    CGFloat toRed;
    CGFloat toGreen;
    CGFloat toBlue;
    CGFloat toAlpha;

    [toColor getRed:&toRed green:&toGreen blue:&toBlue alpha:&toAlpha];

    CGFloat diffRed = toRed - fromRed;
    CGFloat diffGreen = toGreen - fromGreen;
    CGFloat diffBlue = toBlue - fromBlue;
    CGFloat diffAlpha = toAlpha - fromAlpha;

    NSMutableArray *colorArray = [NSMutableArray array];

    [colorArray addObject:fromColor];

    for (NSUInteger i = 0; i < steps - 1; ++i) {
        CGFloat red = fromRed + diffRed / steps * (i + 1);
        CGFloat green = fromGreen + diffGreen / steps * (i + 1);
        CGFloat blue = fromBlue + diffBlue / steps * (i + 1);
        CGFloat alpha = fromAlpha + diffAlpha / steps * (i + 1);

        UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
        [colorArray addObject:color];
    }

    [colorArray addObject:toColor];

    [self animateWithArray:colorArray];
}

- (void)animateWithArray:(NSMutableArray *)array
{
    NSUInteger counter = 0;

    for (UIColor *color in array) {
        double delayInSeconds = STEP_DURATION * counter++;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [UIView animateWithDuration:STEP_DURATION animations:^{
                self.navigationController.navigationBar.barTintColor = color;
            }];
        });
    }
}

缺点:它不光滑的奶油:(

Drawback: It's not buttery smooth :(

这篇关于过渡色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆