【问题标题】:CGContextDrawPath only strokes and does not fillCGContextDrawPath 只描边不填充
【发布时间】:2015-03-25 06:40:52
【问题描述】:

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

这是我的代码:

- (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);
}

这是它现在的样子:

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

我该如何解决这个问题?

【问题讨论】:

    标签: ios objective-c core-graphics cgcontextdrawpath


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-03-13
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多