CGContextRef的渐变颜色

时间:2013-09-12 05:32:32

标签: ios core-graphics

我使用CGContextRef绘制4个弧。我可以填充颜色,但现在我想设置渐变颜色到那个弧。我做了它。它工作正常。现在我想为每个弧设置不同的颜色。是否有任何颜色这样做的方法?请帮帮我。我附上我的代码和那个arc的截图。代码:

CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();

CGFloat comps[] = {1.0,1.0,1.0,1.0,0.0,1.0,1.0,1.0};
CGFloat locs[] = {0,1};
CGGradientRef g = CGGradientCreateWithColorComponents(space, comps, locs, 2);

CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat startAngle = 0;
CGFloat endAngle = 90;
CGFloat radius = 100.0;
CGFloat levelWidth = 50;
for (int i = 0; i < 4; i++) {
    CGContextMoveToPoint(context, self.bounds.size.width/2, self.bounds.size.height/2);
    CGContextAddArc(context, self.bounds.size.width/2, self.bounds.size.height/2, radius, DEGREES_RADIANS(startAngle), DEGREES_RADIANS(endAngle), NO);
    CGContextAddArc(context, self.bounds.size.width/2, self.bounds.size.height/2, radius - levelWidth, DEGREES_RADIANS(endAngle),DEGREES_RADIANS(startAngle), YES);
    startAngle = endAngle;
    endAngle = endAngle + 90;
    CGContextClosePath(context);

}


//CGContextClosePath(context);
CGContextClip(context);
CGContextDrawRadialGradient(context, g, CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2), 1.0f, CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2), 150, 0);

屏幕截图:enter image description here

1 个答案:

答案 0 :(得分:0)

您希望在剪切,剪裁和绘制圆弧之前使用CGContextSaveGState保存上下文,然后在绘制下一个圆弧之前使用CGContextRestoreGState恢复上下文。你显然想要一个CGGradientRef数组。因此:

CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();

CGGradientRef g[4];

CGFloat comps0[] = {1.0,1.0,1.0,1.0,0.0,1.0,1.0,1.0};
CGFloat locs[] = {0,1};
g[0] = CGGradientCreateWithColorComponents(space, comps0, locs, 2);

CGFloat comps1[] = {1.0,1.0,1.0,1.0,1.0,0.0,1.0,1.0};
g[1] = CGGradientCreateWithColorComponents(space, comps1, locs, 2);

CGFloat comps2[] = {1.0,1.0,1.0,1.0,1.0,1.0,0.0,1.0};
g[2] = CGGradientCreateWithColorComponents(space, comps2, locs, 2);

CGFloat comps3[] = {1.0,1.0,1.0,1.0,0.5,0.0,0.5,1.0};
g[3] = CGGradientCreateWithColorComponents(space, comps3, locs, 2);

CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat startAngle = 0;
CGFloat endAngle = 90;
CGFloat radius = 100.0;
CGFloat levelWidth = 50;
for (int i = 0; i < 4; i++) {
    CGContextSaveGState(context);
    CGContextMoveToPoint(context, self.bounds.size.width/2, self.bounds.size.height/2);
    CGContextAddArc(context, self.bounds.size.width/2, self.bounds.size.height/2, radius, DEGREES_RADIANS(startAngle), DEGREES_RADIANS(endAngle), NO);
    CGContextAddArc(context, self.bounds.size.width/2, self.bounds.size.height/2, radius - levelWidth, DEGREES_RADIANS(endAngle),DEGREES_RADIANS(startAngle), YES);
    startAngle = endAngle;
    endAngle = endAngle + 90;
    CGContextClosePath(context);
    CGContextClip(context);
    CGContextDrawRadialGradient(context, g[i], CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2), 1.0f, CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2), 150, 0);
    CGContextRestoreGState(context);
    CGGradientRelease(g[i]);         // don't forget to release the gradient
}

radial quadrants