我正在使用CGContextRef绘制4个圆弧。我可以填充颜色,但是现在我想为这些圆弧设置渐变颜色。我做到了。它工作正常。现在我想为每个圆弧设置不同的颜色。做到这一点的方法?请帮助我。我附上了这段arc.Code的代码和屏幕截图:
I am drawing 4 arcs using CGContextRef.I can fill colour but now i want to set gradient color to that arcs.I did it.it is working fine.Now i want to set different color to each arc.Is there any way to do it?please help me.I am attaching my code and screenshot of that 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);然后截屏:
推荐答案您要使用 CGContextSaveGState 在剪切,剪切和绘制弧之前,然后使用 CGContextRestoreGState 恢复上下文,然后绘制下一条弧。而且您显然想要一个 CGGradientRef 数组。因此:
You want to save the context with CGContextSaveGState before you clip, clip and draw the arc, and then restore the context with CGContextRestoreGState before drawing the next arc. And you obviously want an array of CGGradientRef. Thus:
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 }
更多推荐
渐变颜色到CGContextRef
发布评论