2015-01-26 100 views
0

我在Objective-C中绘制了UIBezier路径,并用红色填充它。现在,我想根据百分比用多种颜色填充路径。例如:我想用20%的绿色填充路径,剩下的80%用红色填充(相互之间不是梯度)。我还希望填充和笔画之间有几个像素的间距。iOS - 根据百分比填充多种颜色的bezierPath

我不知道我该如何完成这些事情。有谁知道我可以如何实现这一点,或者将我指向正确的方向?

非常感谢提前!

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

你的问题还不清楚。您是否想用20%绿色和80%红色混合的单一颜色填充路径?或者你想用绿色填充路径内20%的像素,其余80%的像素用红色? – 2015-01-26 20:57:34

+0

对不起;我想用绿色填充路径内20%的像素,其余80%的像素用红色 – user3767163 2015-01-26 21:39:30

回答

1

这里是你如何做到这一点,我已经将路径分成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); 
+0

非常感谢!你是否也知道如何在填色和笔画之间获得一点余量? – user3767163 2015-01-26 22:11:11

+0

填充后两次行程。首先用较大的线条宽度和白色笔触颜色对其进行描边。然后用窄线宽和黑色笔触描边。 – 2015-01-26 23:06:48

+0

非常感谢! – user3767163 2015-01-29 12:06:50