形状不规则的UIBezierPath填充

wcz*_*ski 2 iphone quartz-graphics cgpath ios uibezierpath

我正在制作一些自定义控件.控件的其中一个子视图是透明的,UIView其中UIBezierPath绘制了路径.在其中一张照片上,我们可以说是UIBezierPath(这里称为[路径描边])的边界,但是在第二张照片上你可以看到当我调用[路径填充]时会发生什么.我希望填充的路径可以创建类似于第一张照片上的形状.drawRect这个透明的UIView方法如下.

 - (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];

 }
Run Code Online (Sandbox Code Playgroud)

感谢帮助 !

第一张图片 第二张图片

jrt*_*ton 5

尝试删除所有moveToPoint调用.这是一个连续的形状,因此它们不是必需的,并且它们使填充算法混乱.您可以从左上角开始,顺时针绘制圆弧,绘制线段(?),逆时针绘制圆弧,然后绘制最后一个线段(或关闭路径).这将填写正确.