我正在创建一个渐变,其中包含我在另一个stackoverflow post中找到的关于在iOS中绘制径向渐变的代码
- (void)drawInContext:(CGContextRef)ctx
{
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"theme"] isEqualToString:@"black"]) {
}
size_t gradLocationsNum = 2;
CGFloat gradLocations[2] = {0.0f, 1.0f};
CGFloat gradColors[8] = {156/255.0f, 97/255.0f, 249/255.0f,1.0f,41/255.0f,131/255.0f,242/255.0f,1.0f};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradColors, gradLocations, gradLocationsNum);
CGColorSpaceRelease(colorSpace);
CGPoint gradCenter= CGPointMake(self.bounds.size.width/2, self.bounds.size.height+100);
float gradRadius = MAX(self.bounds.size.width , self.bounds.size.height) ;
CGContextDrawRadialGradient(ctx, gradient, gradCenter, 0, gradCenter, gradRadius, kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient);
}
我不知道如何定义CGFloat gradColors[8] = {156/255.0f, 97/255.0f, 249/255.0f,1.0f,41/255.0f,131/255.0f,242/255.0f,1.0f};
,以便我可以执行以下操作:
CGFloat gradColors[8];
if ...
gradColors[8] = {156/255.0f, 97/255.0f, 249/255.0f,1.0f,41/255.0f,131/255.0f,242/255.0f,1.0f};
else if ...
gradColors[8] = {156/255.0f, 97/255.0f, 249/255.0f,1.0f,41/255.0f,131/255.0f,242/255.0f,1.0f};
另外:有没有办法将UIColor
转换为这种格式?
答案 0 :(得分:1)
您可以尝试使用接受CGColor数组的CGGradientCreateWithColors()
。
例如
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)@[(id)aUIColor1.CGColor, (id)aUIColor2.CGColor], gradientLocations);