2015-01-26 95 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