我正在使用 CGContextRef 绘制 4 条弧线。我可以填充颜色,但现在我想为该弧线设置渐变颜色。我做到了。它工作正常。现在我想为每个弧线设置不同的颜色。有什么办法吗它?请帮助我。我附上了我的代码和该 arc.Code 的屏幕截图:
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);
和屏幕截图: