鉴于第一篇文章。写了下面的颜色过渡动画
//颜色过渡
+(UIColor *)currentColor:(UIColor*)color1 changeColor:(UIColor *)color2 ratio:(CGFloat)ratio
{
CGFloat components1[3];
CGFloat components2[3];
[self getRGBComponents:components1 forColor:color1];
[self getRGBComponents:components2 forColor:color2];
CGFloat r = components1[0]*ratio + components2[0]*(1-ratio);
CGFloat g = components1[1]*ratio + components2[1]*(1-ratio);
CGFloat b = components1[2]*ratio + components2[2]*(1-ratio);
return [UIColor colorWithRed:r green:g blue:b alpha:1];
}
//获取components;
+ (void)getRGBComponents:(CGFloat [3])components forColor:(UIColor *)color {
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char resultingPixel[4];
CGContextRef context = CGBitmapContextCreate(&resultingPixel,
1,
1,
8,
4,
rgbColorSpace,
kCGImageAlphaNoneSkipLast);
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
CGContextRelease(context);
CGColorSpaceRelease(rgbColorSpace);
for (int component = 0; component < 3; component++) {
components[component] = resultingPixel[component] / 255.0f;
}
}