如何绘制渐变填充弧,即彩虹? [英] How can I draw a gradient filled arc, i.e. rainbow?

查看:28
本文介绍了如何绘制渐变填充弧,即彩虹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码绘制一个从红色到绿色渐变的半圆.这不是我想要的.我期望用渐变绘制的宽度为5像素的弧.

The following code draws a half circle with a gradient from red to green. This is not what I wanted. I expected an arc of width 5 pixels that is painted with the gradient.

在显示我出问题的地方方面的任何帮助将不胜感激.

Any help in showing where I've gone wrong will be greatly appreciated.

查尔斯

-(void) DrawRainbow {
// Create an arc path
float x = 150.0;
float y = 220.0;
float radius = 75.0;
float startAngle = M_PI;
float endAngle   = 2*M_PI;
bool clockWise = false;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, nil, x, y, radius, startAngle, endAngle, clockWise);

// Setup the gradient
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = {
    1.0, 0.0, 0.0, 1.0,   // Start color is red
    0.0, 1.0, 0.0, 1.0 }; // End color is green
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradientFill = 
    CGGradientCreateWithColorComponents (colorSpace, components,
                                         locations, num_locations);
// setup gradient points
CGRect pathRect = CGPathGetBoundingBox(path);
CGPoint myStartPoint, myEndPoint;
myStartPoint.x = CGRectGetMinX(pathRect);
myStartPoint.y = CGRectGetMinY(pathRect);
myEndPoint.x   = CGRectGetMaxX(pathRect);
myEndPoint.y   = CGRectGetMinY(pathRect);

// draw the gradient
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 5.0);
CGContextAddPath(context, path);
CGContextSaveGState(context);
CGContextClip(context);
CGContextDrawLinearGradient (context, gradientFill,
                             myStartPoint, myEndPoint, 0);
CGContextRestoreGState(context);

CGGradientRelease(gradientFill);
CGColorSpaceRelease(colorSpace);

}

推荐答案

我认为您想要一个径向渐变.请参阅此Apple文档.

I think you want a radial gradient. See this Apple document.

https://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_shadings/dq_shadings.html

这篇关于如何绘制渐变填充弧,即彩虹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆