gpt4 book ai didi

iOS - 根据百分比使用多种颜色填充 bezierPath

转载 作者:行者123 更新时间:2023-11-29 10:33:45 25 4
gpt4 key购买 nike

我在 Objective-C 中绘制了一个 UIBezierPath 并用红色填充它。现在,我想根据百分比用多种颜色填充路径。例如:我想用 20% 的绿色填充路径,用红色填充剩余的 80%,彼此叠加(不是渐变)。我还希望填充和描边之间有几个像素的间距。

我不知道如何才能完成这些事情。有谁知道我如何才能做到这一点或指出正确的方向?

非常感谢!

UIBezierPath* bezierPath = UIBezierPath.bezierPath;
[bezierPath moveToPoint: CGPointMake(50, 50)];
[bezierPath addLineToPoint: CGPointMake(60, 90)];
[bezierPath addLineToPoint: CGPointMake(80, 90)];
[bezierPath addLineToPoint: CGPointMake(90, 50)];

bezierPath.lineCapStyle = kCGLineCapRound;
bezierPath.lineJoinStyle = kCGLineJoinBevel;

[UIColor.redColor setFill];
[bezierPath fill];
[UIColor.blackColor setStroke];
bezierPath.lineWidth = 4;
[bezierPath stroke];

最佳答案

这里是你如何做到这一点,我将路径分为 5 个部分,每个部分占 20%。

enter image description here

 UIBezierPath* bezierPath = UIBezierPath.bezierPath;
[bezierPath moveToPoint: CGPointMake(50, 50)];
[bezierPath addLineToPoint: CGPointMake(60, 90)];
[bezierPath addLineToPoint: CGPointMake(80, 90)];
[bezierPath addLineToPoint: CGPointMake(90, 50)];

bezierPath.lineCapStyle = kCGLineCapRound;
bezierPath.lineJoinStyle = kCGLineJoinBevel;

[UIColor.redColor setFill];
bezierPath.lineWidth = 4;
[bezierPath stroke];
[bezierPath addClip];

CGRect boundingBox = CGPathGetBoundingBox(bezierPath.CGPath);

CGRect firstTwentyPercent = CGRectMake(boundingBox.origin.x,
boundingBox.origin.y + boundingBox.size.height - 0.2 * boundingBox.size.height,
boundingBox.size.width,
0.2 * boundingBox.size.height);
[[UIColor greenColor] setFill];
UIRectFill(firstTwentyPercent);

CGRect secondTwentyPercent = CGRectMake(boundingBox.origin.x,
boundingBox.origin.y + boundingBox.size.height - 0.4 * boundingBox.size.height,
boundingBox.size.width,
0.2 * boundingBox.size.height);
[[UIColor blueColor] setFill];
UIRectFill(secondTwentyPercent);


CGRect thirdTwentyPercent = CGRectMake(boundingBox.origin.x,
boundingBox.origin.y + boundingBox.size.height - 0.6 * boundingBox.size.height,
boundingBox.size.width,
0.2 * boundingBox.size.height);
[[UIColor redColor] setFill];
UIRectFill(thirdTwentyPercent);


CGRect fourthTwentyPercent = CGRectMake(boundingBox.origin.x,
boundingBox.origin.y + boundingBox.size.height - 0.8 * boundingBox.size.height,
boundingBox.size.width,
0.2 * boundingBox.size.height);
[[UIColor cyanColor] setFill];
UIRectFill(fourthTwentyPercent);

CGRect fifthTwentyPercent = CGRectMake(boundingBox.origin.x,
boundingBox.origin.y,
boundingBox.size.width,
0.2 * boundingBox.size.height);
[[UIColor orangeColor] setFill];
UIRectFill(fifthTwentyPercent);

关于iOS - 根据百分比使用多种颜色填充 bezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28158549/

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