UIBezierPaths不会保留在屏幕上

时间:2013-05-01 13:45:40

标签: ios objective-c uibezierpath

我有一个自定义的UIView,在drawRect方法中我创建了一个UIBezierPath并将其渲染到屏幕上。我遇到的问题是先前的UIBezierPath在下一次drawRect调用中从视图中删除了吗?

如何在屏幕上保留所有这些UIBezierPath?

- (void)drawRect:(CGRect)rect
{
    UIBezierPath *path = [UIBezierPath bezierPath];

    // Move to centre and draw an arc.
    [path moveToPoint:self.center];
    [path addArcWithCenter:self.center
                radius:self.radius
            startAngle:self.startAngle
              endAngle:self.endAngle
             clockwise:YES];

    [path closePath];

    path.usesEvenOddFillRule = YES;

    [self.colorToRender setFill];
    [path fill];
}

2 个答案:

答案 0 :(得分:0)

每当调用drawRect:时,您必须绘制整个内容屏幕。您无法将新图纸添加到已存在的图纸中。所以你需要存储整个beziers列表并绘制所有这些。

将它们存储在一个数组中,然后遍历数组并绘制每个数组。

此外,您不应该在drawRect: ...

中创建新的beziers

答案 1 :(得分:0)

我不确定这是否是最方便的方法,但您必须继续引用旧路径,然后将新路径与它们合并。

CAShapeLayer * shapeLayer; //Sublayer of your view
CGMutablePathRef combinedPath = CGPathCreateMutableCopy(shapeLayer.path);

CGMutablePathRef linePath = CGPathCreateMutable();

//Create your own custom linePath here

//No paths drawn before
if(combinedPath == NULL)
{
    combinedPath = linePath;
}
else
{
    CGPathAddPath(combinedPath, NULL, linePath);
}
shapeLayer.path = combinedPath;
CGPathRelease(linePath);
相关问题