如何使用Core Graphics / iPhone绘制渐变弧? [英] How to draw a gradient arc with Core Graphics/iPhone?
问题描述
我知道如何绘制圆弧
我还发现如何从
为了我的目的,我只使用了绘图和渐变部分那个答案。结构看起来或多或少像这样......
CGContextRef context = UIGraphicsGetCurrentcontext();
CGFloat arcStartAngle = M_PI;
CGFloat arcEndAngle = 2 * M_PI;
CGPoint startPoint = CGPointMake(...);
CGPoint endPoint = CGPointMake(...);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat colors [] =
{
1.0,0.0,0.0,1.0,// RGBA值(在这种情况下为红色到绿色)
0.0,1.0 ,0.0,1.0
};
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace,colors,NULL,2);
//其中2表示颜色分量的数量。通过添加colors []数组和更改组件值,您可以在整个梯度中获得更多颜色。
CGColorSpaceRelease(colorSpace);
//现在为弧形部分...
CGMutablePathRef arc = CGPathCreateMutable();
CGPathMoveToPoint(arc,NULL,startPoint.x,startPoint.y);
//这里,CGPoint self.arcCenter是放置弧的点,所以可能是
//视图的中间位置。 self.radius是此中心点与弧之间的距离。
CGPathAddArc(arc,NULL,self.arcCenter.x,self.arcCenter.y,self.radius,
arcStartAngle,arcEndAngle,YES);
//这实际上沿着弧形路径绘制
CGPathRef strokedArc = CGPathCreateCopyByStrokingPath(arc,NULL,5.0f,
kCGLineCapButt,kCGLineJoinMiter,10) ;
CGContextSaveGState(context);
CGContextAddPath(context,strokedArc);
CGContextClip(context);
CGContextDrawLinearGradient(context,gradient,startPoint,endPoint,0);
CGContextDrawPath(context,kCGPathFillStroke);
CGGradientRelease(渐变);
CGContextRestoreGState(context);
//这一切都绘制了一个比弧本身大得多的渐变,但使用
// CGContextClip,除了弧线中的颜色外,它会剪切掉所有内容。保存和恢复
//状态允许您保留正在进行的任何其他绘图。如果您没有使用这些,
//那么所有其他绘图也会被剪裁。
我希望这会有所帮助。如果其中任何一项不清楚,我建议您查看上面的问题链接。这些问题的答案包含了我在这个答案中使用的所有内容以及一些更酷和有用的绘图技巧。
I know how to draw a arc
and I also found how to draw a gradient line from here I have found two function can draw gradient:CGContextDrawLinearGradient and CGContextDrawRadialGradient.but how can I draw a gradient arc? I want to realize like this picture:I spent a long time searching for how to do this, too, so I thought I'd post the way I ended up doing it. It turns out both answers are in the excellent answer to this question:
Draw segments from a circle or donut
For my purposes, I only used the drawing and gradient parts of that answer. The structure looks more or less like this...
CGContextRef context = UIGraphicsGetCurrentcontext();
CGFloat arcStartAngle = M_PI;
CGFloat arcEndAngle = 2 * M_PI;
CGPoint startPoint = CGPointMake(...);
CGPoint endPoint = CGPointMake(...);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat colors[] =
{
1.0, 0.0, 0.0, 1.0, //RGBA values (so red to green in this case)
0.0, 1.0, 0.0, 1.0
};
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colors, NULL, 2);
//Where the 2 is for the number of color components. You can have more colors throughout //your gradient by adding to the colors[] array, and changing the components value.
CGColorSpaceRelease(colorSpace);
//Now for the arc part...
CGMutablePathRef arc = CGPathCreateMutable();
CGPathMoveToPoint(arc, NULL, startPoint.x, startPoint.y);
//Here, the CGPoint self.arcCenter is the point around which the arc is placed, so maybe the
//middle of your view. self.radius is the distance between this center point and the arc.
CGPathAddArc(arc, NULL, self.arcCenter.x, self.arcCenter.y, self.radius,
arcStartAngle, arcEndAngle, YES);
//This essentially draws along the path in an arc shape
CGPathRef strokedArc = CGPathCreateCopyByStrokingPath(arc, NULL, 5.0f,
kCGLineCapButt, kCGLineJoinMiter, 10);
CGContextSaveGState(context);
CGContextAddPath(context, strokedArc);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextDrawPath(context, kCGPathFillStroke);
CGGradientRelease(gradient);
CGContextRestoreGState(context);
//This all draws a gradient that is much larger than the arc itself, but using
//CGContextClip, it clips out everything EXCEPT the colors in the arc. Saving and Restoring
//the state allows you to preserve any other drawing going on. If you didn't use these,
//then all other drawing would also be clipped.
I hope this helps. If any of this is unclear, I recommend you check out the question link above. The answer to that questions contains everything I used in this answer and a few more cool and useful drawing tips.
这篇关于如何使用Core Graphics / iPhone绘制渐变弧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!