UITabBar 默认绘制一个微妙的渐变:
我想用任何给定的 tintColor 在我的代码中复制这种外观和感觉。明确一点:我不想在 UITabBar 上设置 tintColor(从 iOS 5 开始可以),我想在我自己的 UIView 中绘制渐变。
我知道如何绘制渐变,我的问题是如何从 tintColor 中导出渐变的颜色。我正在考虑获取颜色的亮度并生成具有不同亮度设置的其他颜色,但这似乎效果不佳,并且看起来不像我希望的那样好。
我需要我的开源 TabBarController 的代码:https ://github.com/NOUSguide/NGTabBarController
这是我当前创建渐变的代码:
UIColor *baseColor = self.tintColor;
CGFloat hue, saturation, brightness, alpha;
// TODO: Only works on iOS 5
[baseColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
// That's the question, how to compute the colors ...
NSArray *colors = [NSArray arrayWithObjects:
[UIColor colorWithHue:hue saturation:saturation brightness:brightness+0.2 alpha:alpha],
[UIColor colorWithHue:hue saturation:saturation brightness:brightness+0.15 alpha:alpha],
[UIColor colorWithHue:hue saturation:saturation brightness:brightness+0.1 alpha:alpha],
baseColor, nil];
NSUInteger colorsCount = colors.count;
CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors objectAtIndex:0] CGColor]);
NSArray *locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.25],
[NSNumber numberWithFloat:0.49],
[NSNumber numberWithFloat:0.5], nil];
CGFloat *gradientLocations = NULL;
NSUInteger locationsCount = locations.count;
gradientLocations = (CGFloat *)malloc(sizeof(CGFloat) * locationsCount);
for (NSUInteger i = 0; i < locationsCount; i++) {
gradientLocations[i] = [[locations objectAtIndex:i] floatValue];
}
NSMutableArray *gradientColors = [[NSMutableArray alloc] initWithCapacity:colorsCount];
[colors enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
[gradientColors addObject:(id)[(UIColor *)object CGColor]];
}];
_gradientRef = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
if (gradientLocations) {
free(gradientLocations);
}