试图填充用贝塞尔曲线绘制的图形颜色

时间:2014-04-17 18:29:08

标签: ios

我第一次尝试使用UIBezierPath,并试图填充我用彩色绘制的图表,但没有成功。这就是我在自定义UIView中所做的:

- (void)calculateGraphSize
{
    self.topY = CGRectGetMinY(self.bounds);
    self.bottomY = CGRectGetMaxY(self.bounds);
    self.minX = CGRectGetMinX(self.bounds);
    self.maxX = CGRectGetMaxX(self.bounds);
}

- (void) drawRect: (CGRect) rect
{
    CGSize cornerRadius = CGSizeMake(10.0f, 10.0f);

    [self calculateGraphSize];

    UIBezierPath *graphBorder = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:cornerRadius];
    [[UIColor redColor] setFill];
    [graphBorder fill];


    UIBezierPath *barGraph = [UIBezierPath bezierPath];
    barGraph.lineWidth = 2.0f;

    CGPoint point = CGPointMake(self.minX, self.bottomY);
    [barGraph moveToPoint:point];

    point = CGPointMake(self.minX + 50, self.bottomY - 50);
    [barGraph addLineToPoint:point];

    point = CGPointMake(self.minX + 100, self.bottomY - 75);
    [barGraph addLineToPoint:point];

    point = CGPointMake(self.minX + 200, self.bottomY);
    [barGraph addLineToPoint:point];

    [[UIColor blackColor] setStroke];
    [[UIColor greenColor] setFill];
    [barGraph closePath];
    [barGraph stroke];
}

1 个答案:

答案 0 :(得分:0)

只需添加[barGraph fill]

[[UIColor blackColor] setStroke];
[[UIColor greenColor] setFill];
[barGraph closePath];
[barGraph fill];
[barGraph stroke];

你可能想要填充然后抚摸,因为我不记得填充是否会掩盖中风(我总是要检查哈!)