這裏是你如何做到這一點,我已經將路徑分成5部分,每部分20%。
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);
你的問題還不清楚。您是否想用20%綠色和80%紅色混合的單一顏色填充路徑?或者你想用綠色填充路徑內20%的像素,其餘80%的像素用紅色? – 2015-01-26 20:57:34
對不起;我想用綠色填充路徑內20%的像素,其餘80%的像素用紅色 – user3767163 2015-01-26 21:39:30