由于您将帖子与示例代码相关联,因此我对其进行了一些修改,以使其与您可能想要做的事情相对应。
主要思想是,当您使用 UIBezierPath 绘制轮子时,您必须提供您想要在屏幕上绘制的颜色。
如果您查看您提供的链接中的代码 (https://stackoverflow.com/a/16735555/5226245),您将在 viewDidLoad 方法中看到以下代码行:
// ...
// You create a color and then use it to color a small part of the wheel
UIColor *color = [UIColor colorWithHue:((float)i)/sectors saturation:1. brightness:1. alpha:1];
[color setFill];
// ...
这是您要调整方法的地方。
所以你所要做的就是 提供你自己的颜色!简而言之,您必须提供“足够”的颜色(不是 10-20,但更像是链接中的 >100 或 180),否则您的色轮渐变将不平滑。
为此,您只需更改一行代码。
// replace
// UIColor *color = [UIColor colorWithHue:((float)i)/sectors saturation:1. brightness:1. alpha:1];
// with something like
UIColor *color = [self provideWarmColorSliceForSlice:( i ) slicesCount:(sectors)];
// the variable is then automatically handled further in the sample.
provideWarmColorSliceForSlice:slicesCount: 方法可以定义如下:
- (UIColor *)provideWarmColorSliceForSlice:(NSInteger)sliceNo slicesCount:(NSInteger)totalSlices {
// I want to cover the interval [200; 255] U [0; 40] which contains the warmest colors
int warmHueStart = 200;
int warmHueEnd = 40;
// If you want the standard color wheel, just use
// warmHueStart=254; warmHueEnd = 253; // interval [254; 255] U [0; 253]
// code to compute the hue
int l = (255-warmHueStart) + warmHueEnd;
float inputRatio = ((float)sliceNo)/totalSlices;
float targetedHue = (float) ((warmHueStart + ((int)(inputRatio * l)) ) % 255);
float hue = targetedHue / 255.0f;
return [UIColor colorWithHue:hue saturation:1.0f brightness:1.0f alpha:1.0f];
}
瞧!仅此而已,但是更多的代码重用,您会很高兴(好吧,如果答案没有迟到的话:j)