gpt4 book ai didi

iOS - 如何沿路径绘制渐变

转载 作者:可可西里 更新时间:2023-11-01 04:55:26 27 4
gpt4 key购买 nike

这是我想要达到的效果:

enter image description here

这是我现在的效果:

enter image description here

基本上,我需要沿着圆弧的路径绘制渐变,而不是线性穿过圆弧。我尝试了很多方法,有的结果是一样的,有的结果却与预期相去甚远。

这是我现在拥有的代码:

    CGFloat lineSize = 12;
CGFloat radius = self.frame.size.width/2 - lineSize;

CGContextRef ctx = UIGraphicsGetCurrentContext();

//Define gradient
CGFloat colors [] =
{
0.9451f, 0.5804f, 0.2431f, 1.0f,
0.9922f, 0.9490f, 0.9020f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
0.8706f, 0.9020f, 0.9412f, 1.0f,
0.2941f, 0.6863f, 0.9176f, 1.0f
};

CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, colors, NULL, 5);
CGColorSpaceRelease(baseSpace), baseSpace = NULL;

//Define arc angle and points
CGFloat startAngle = 3.0f/4.0f * M_PI;
CGFloat endAngle = 1.0f/4.0f * M_PI;

CGFloat a = radius * cos(45);
CGFloat o = a * tan(45);

CGFloat y = CGRectGetMidY(rect) + o;

CGPoint startPoint = CGPointMake(0, y);
CGPoint endPoint = CGPointMake(self.frame.size.width, y);

CGMutablePathRef arc = CGPathCreateMutable();
CGPathAddArc(arc, NULL, CGRectGetMidX(rect), CGRectGetMidY(rect), radius,
startAngle, endAngle, NO);

CGPathRef strokedArc = CGPathCreateCopyByStrokingPath(arc, NULL, 10, kCGLineCapButt, kCGLineJoinMiter, 10);

CGContextAddPath(ctx, strokedArc);
CGContextClip(ctx);

CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, 0);
CGGradientRelease(gradient);

我还尝试更改此代码块:

CGMutablePathRef arc = CGPathCreateMutable();
CGPathAddArc(arc, NULL, CGRectGetMidX(rect), CGRectGetMidY(rect), radius,
startAngle, endAngle, NO);

CGPathRef strokedArc = CGPathCreateCopyByStrokingPath(arc, NULL, 10, kCGLineCapButt, kCGLineJoinMiter, 10);

CGContextAddPath(ctx, strokedArc);
CGContextClip(ctx);

CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, 0);
CGGradientRelease(gradient);

此 block 代码:

CGContextAddArc(ctx, CGRectGetMidX(rect), CGRectGetMidY(rect), radius, startAngle, endAngle, NO);

CGContextSetLineWidth(ctx, lineSize);
CGContextSetLineCap(ctx, kCGLineCapRound);

CGContextReplacePathWithStrokedPath(ctx);
CGContextClip(ctx);

CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, 0);
CGGradientRelease(gradient), gradient = NULL;

但仍然会产生与我现在的结果相似的结果。

最佳答案

尝试改变渐变值。

改变

 //Define gradient
CGFloat colors [] =
{
0.9451f, 0.5804f, 0.2431f, 1.0f,
0.9922f, 0.9490f, 0.9020f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
0.8706f, 0.9020f, 0.9412f, 1.0f,
0.2941f, 0.6863f, 0.9176f, 1.0f
};

对某些对称值。喜欢

    CGFloat colors [] =
{
1.0f, 0.9176f, 0.6863f, 0.2941f,
1.0f, 0.9412f, 0.9020f, 0.8706f,
0.7432f, 1.0f, 1.0f, 0.7432f,
0.8706f, 0.9020f, 0.9412f, 1.0f,
0.2941f, 0.6863f, 0.9176f, 1.0f
};

关于iOS - 如何沿路径绘制渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45211908/

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