0

我正在尝试制作一个可以描边和填充但不填充的形状。

这是我的代码:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    CGMutablePathRef pathRef = CGPathCreateMutable();
    CGContextBeginPath(ctx);

    self.dottedLineColor = [UIColor whiteColor];
    self.solidLineColor = [UIColor blackColor];
    CGContextSetStrokeColorWithColor(ctx, [self.dottedLineColor CGColor]);
    CGContextSetFillColorWithColor(ctx, [UIColor blackColor].CGColor);

    CGContextSetLineWidth(ctx, 11.0);

    for (NSArray *array in previewPoints){
            CGPoint pointMadeFromString1 = CGPointFromString([array objectAtIndex:0]);
            CGPoint pointMadeFromString2 = CGPointFromString([array objectAtIndex:1]);

            CGPathMoveToPoint(pathRef, NULL, pointMadeFromString1.x, pointMadeFromString1.y);
            CGPathAddLineToPoint(pathRef, NULL, pointMadeFromString2.x, pointMadeFromString2.y);
            /*
             CGContextMoveToPoint(ctx, pointMadeFromString1.x, pointMadeFromString1.y);
             CGContextAddLineToPoint(ctx, pointMadeFromString2.x, pointMadeFromString2.y);

             CGContextSetLineWidth(ctx, 11.0);
             const CGFloat dashPattern[2] = {2, 0};
             CGContextSetLineDash(ctx, 2, dashPattern, 2);
             CGContextStrokePath(ctx);
             */
    }
    CGContextAddPath(ctx, pathRef);
    CGContextClosePath(ctx);

    CGContextDrawPath(ctx, kCGPathFillStroke);
    CGContextFillPath(ctx);
}

这是它现在的样子:

在此处输入图像描述

中间的区域应该是黑色的。

我将如何解决这个问题?

4

1 回答 1

2

问题是每次我使用CGPathMoveToPoint这意味着它无法填充,因为它实际上不是一条封闭的路径。我切换了它,以便只有数组中的第一个项目会使用CGPathMoveToPoint,然后所有其他项目都会使用CGPathAddLineToPoint.

于 2015-03-25T15:20:07.107 回答