gpt4 book ai didi

ios - 如何绘制渐变填充弧,即彩虹?

转载 作者:可可西里 更新时间:2023-11-01 04:03:53 25 4
gpt4 key购买 nike

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

如能帮助我指出错误之处,我们将不胜感激。

查尔斯

-(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);

最佳答案

关于ios - 如何绘制渐变填充弧,即彩虹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2821440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com