mye*_*l0w 4 drawing gradient colors drawrect ios
默认情况下,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);
}
Run Code Online (Sandbox Code Playgroud)
这是一个很好的教程: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
| 归档时间: |
|
| 查看次数: |
2484 次 |
| 最近记录: |