填充路径内部保持线宽可见

时间:2013-07-16 12:06:04

标签: iphone ios core-graphics quartz-graphics cgpath

我试图填充形状的内部,但没有用。它只会描边路径但不填充hello颜色。我用谷歌搜索,但没有任何作品填补不起作用。指导我做错了什么。

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddPath(ctx, pathi);
CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextSetFillColorWithColor(ctx, [UIColor yellowColor].CGColor);
CGContextSetLineWidth(ctx, 3.0);
CGContextSetLineCap(ctx, kCGLineCapSquare);
CGContextSetAllowsAntialiasing(ctx, NO);
// CGContextStrokePath(ctx);
CGContextDrawPath(ctx, kCGPathFillStroke);

编辑:pathi的代码是

- (CGMutablePathRef)getPath {

if (self.shapeType == NOSHAPE) {
    return nil;
}

[lineHeight removeAllObjects];
[linesArray removeAllObjects];

CGMutablePathRef path = CGPathCreateMutable();

switch (self.shapeType) {
    case Rectangle_SHAPE:{
        Line *l1 = [[Line alloc] initWithLineName:LineName_a_S
                                      starttPoint:self.bound.origin
                                         endPoint:CGPointMake(self.bound.size.width, self.bound.origin.y) scalingFactor:1.0
                                     linePosition:LINE_DIRECTION_HORIZONTAL];

        [lineHeight addObject:[NSNumber numberWithFloat:l1.lineLength]];
        [linesArray addObject:l1];

        Line *l2 = [[Line alloc] initWithLineName:LineName_c_S
                                      starttPoint:CGPointMake(self.bound.size.width, self.bound.origin.y)
                                         endPoint:CGPointMake(self.bound.size.width, self.bound.size.height) scalingFactor:1.0
                                     linePosition:LINE_DIRECTION_VERTICAL];

        [lineHeight addObject:[NSNumber numberWithFloat:l2.lineLength]];
        [linesArray addObject:l2];

        Line *l3 = [[Line alloc] initWithLineName:LineName_b_S
                                      starttPoint:CGPointMake(self.bound.size.width, self.bound.size.height)
                                         endPoint:CGPointMake(self.bound.origin.x, self.bound.size.height) scalingFactor:1.0
                                     linePosition:LINE_DIRECTION_HORIZONTAL];

        [lineHeight addObject:[NSNumber numberWithFloat:l3.lineLength]];
        [linesArray addObject:l3];

        Line *l4 = [[Line alloc] initWithLineName:LineName_d_S
                                      starttPoint:CGPointMake(self.bound.origin.x, self.bound.size.height)
                                         endPoint:self.bound.origin scalingFactor:1.0
                                     linePosition:LINE_DIRECTION_VERTICAL];

        [lineHeight addObject:[NSNumber numberWithFloat:l4.lineLength]];
        [linesArray addObject:l4];

        CGPathAddPath(path, NULL, [l1 getPath]);
        CGPathAddPath(path, NULL, [l2 getPath]);
        CGPathAddPath(path, NULL, [l3 getPath]);
        CGPathAddPath(path, NULL, [l4 getPath]);

        [l1 release];
        [l2 release];
        [l3 release];
        [l4 release];
        break;
    }
    default:
        break;
}

return path;

}

我也在创造其他形状,并且路径以相同的方式制作。 类给出了起点和终点的行。请告诉我是否有遗漏的东西。

1 个答案:

答案 0 :(得分:0)

我也推荐用户普茨在评论中提出的建议:

存储路径,首先填充它(CGContextFillPath),更改描边颜色,然后描边(CGContextStrokePath)。
我已经这样做了,这很好。