2012-01-01 125 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通话。这是一个连续的形状,所以它们不是必需的,而且它们使填充算法混乱。您可以从左上角开始,顺时针绘制圆弧,绘制分段(?)线,逆时针绘制圆弧,然后绘制最后一个分段线(或关闭路径)。这将会正确填充。