iOS - Fill bezierPath with multiple colors based on percentage

荒凉一梦 提交于 2019-12-08 04:55:24

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!