4

UITabBar 默认绘制一个微妙的渐变:

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);
}
4

1 回答 1

5

这是一个很好的教程:http ://www.raywenderlich.com/2079/core-graphics-101-shadows-and-gloss 看看有光泽效果的部分。

也在这里:http ://cocoawithlove.com/2008/09/drawing-gloss-gradients-in-coregraphics.html

还有一个完整的代码示例:http ://www.mlsite.net/blog/?page_id=372

于 2012-04-27T22:03:36.947 回答