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

时间:2015-01-26 20:52:59

标签: ios drawing uibezierpath

我在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];

1 个答案:

答案 0 :(得分:2)

以下是如何做到这一点,我将路径划分为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);