iOS-根据百分比用多种颜色填充bezierPath [英] iOS - Fill bezierPath with multiple colors based on percentage

查看:233
本文介绍了iOS-根据百分比用多种颜色填充bezierPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I drawed a UIBezierPath in Objective-C and fill it with a red color. Now, I want to fill the path with multiple colors based on percentage. For example: I want to fill the path with 20% green color and the remaining 80% with red color, on top of each other (not a gradient). I also want a few pixels spacing between the fill and the stroke.

我不知道如何完成这些任务。有谁知道我能做到这一点或为我指明正确的方向?

I don't know how I can accomplish these things. Does anyone know how I can accomplish this or point me in the right direction?

在此先感谢!

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%。

Here is how you can do this, I have divided the path into 5 parts 20 % of each.

 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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