2012-01-01 107 views
2

我正在做一些自定義控件。其中一個控件的子視圖是透明的UIView,其上繪製的路徑爲UIBezierPath。在其中一張照片上,我們只需說出UIBezierPath的邊框(稱爲[path stroke]),但在第二張照片上,您可以看到當我調用[路徑填充]時會發生什麼。我希望填充的路徑能夠創建與第一張照片相似的形狀。 drawRect方法從這個透明UIView下面。形狀不規則的UIBezierPath填充

- (void)drawRect:(CGRect)rect 
{ 
// Drawing code 
[super drawRect:rect]; 
UIBezierPath *path; 
[[UIColor greenColor] setStroke]; 
[[UIColor greenColor] setFill]; 
//Angles 

CGFloat startAngle = degreesToRadians(225); 
CGFloat endAngle = degreesToRadians(315); 

CGPoint center = CGPointMake(CGRectGetMidX(rect), lRadius); 
path = [UIBezierPath bezierPathWithArcCenter: center radius:lRadius startAngle:startAngle endAngle:endAngle clockwise:YES]; 
rightEndOfLine = path.currentPoint; 
bigArcHeight = rightEndOfLine.y; 


CGFloat smallArcHeight = lRadius - bigArcHeight - sRadius; 
CGFloat smallArcWidthSquare = (8*smallArcHeight*sRadius) - (4 * (smallArcHeight * smallArcHeight)); 
smallArcWidth = sqrtf(smallArcWidthSquare); 


leftStartOfLine = CGPointMake((rect.size.width - smallArcWidth)/2, lRadius-sRadius + smallArcHeight);  
CGFloat lengthOfSpace = (rect.size.width - rightEndOfLine.x); 

[path moveToPoint:leftStartOfLine]; 
[path addArcWithCenter:center radius:sRadius startAngle:startAngle endAngle:endAngle clockwise:YES]; 
rightStartOfLine = path.currentPoint; 
leftEndOfLine = CGPointMake(lengthOfSpace, bigArcHeight); 
[path moveToPoint:rightStartOfLine]; 
[path addLineToPoint:rightEndOfLine]; 
[path moveToPoint:leftStartOfLine]; 
[path addLineToPoint:leftEndOfLine]; 
[path closePath]; 
[path stroke]; 
// [path fill]; 

} 

感謝您的幫助!

First Image Second Image

回答

5

嘗試刪除所有moveToPoint通話。這是一個連續的形狀,所以它們不是必需的,而且它們使填充算法混亂。您可以從左上角開始,順時針繪製圓弧,繪製分段(?)線,逆時針繪製圓弧,然後繪製最後一個分段線(或關閉路徑)。這將會正確填充。