渐变颜色到CGContextRef

编程入门 行业动态 更新时间:2024-10-10 06:20:46
本文介绍了渐变颜色到CGContextRef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用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

本文发布于:2023-11-30 22:38:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651682.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:颜色   CGContextRef

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!