UIColor间的过渡算法

Posted 吴彦祖666

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIColor间的过渡算法相关的知识,希望对你有一定的参考价值。

示例图

代码

- (UIColor *)transformFromColor:(UIColor*)fromColor toColor:(UIColor *)toColor progress:(CGFloat)progress 

    progress = progress >= 1 ? 1 : progress;
    progress = progress <= 0 ? 0 : progress;

    const CGFloat * fromeComponents = CGColorGetComponents(fromColor.CGColor);
    const CGFloat * toComponents = CGColorGetComponents(toColor.CGColor);

    size_t  fromColorNumber = CGColorGetNumberOfComponents(fromColor.CGColor);
    size_t  toColorNumber = CGColorGetNumberOfComponents(toColor.CGColor);

    if (fromColorNumber == 2) 
        CGFloat white = fromeComponents[0];
        fromColor = [UIColor colorWithRed:white green:white blue:white alpha:1];
        fromeComponents = CGColorGetComponents(fromColor.CGColor);
    

    if (toColorNumber == 2) 
        CGFloat white = toComponents[0];
        toColor = [UIColor colorWithRed:white green:white blue:white alpha:1];
        toComponents = CGColorGetComponents(toColor.CGColor);
    

    CGFloat r = fromeComponents[0]*(1 - progress) + toComponents[0]*progress;
    CGFloat g = fromeComponents[1]*(1 - progress) + toComponents[1]*progress;
    CGFloat b = fromeComponents[2]*(1 - progress) + toComponents[2]*progress;

    return [UIColor colorWithRed:r green:g blue:b alpha:1];

以上是关于UIColor间的过渡算法的主要内容,如果未能解决你的问题,请参考以下文章

黑色背景白色自定义过渡

jQuery Mobile 和 PhoneGap:过渡期间的正文背景

过渡期间的测试:外包软件的测试标准(译)

Firefox CSS3 object-fit:覆盖过渡期间的奇怪行为

平滑的颜色过渡算法

CSS过渡动画