在连续调用drawRect时保留先前的UIBezierPath笔划

时间:2014-05-06 11:08:50

标签: ios core-graphics

我想提一下,这篇文章与Continuous drawing in CGContext with drawRect类似,但它没有任何代码段解决方案,所以再次询问。

我正在学习iOS开发应用程序。我正在尝试使用UIBezierPath创建绘图应用程序。目前,只要我有新的触摸和新UIBezierPath,我就会执行以下操作来显示之前的UIBezierPath。如果有更好/推荐的方式,请告诉我。我有颜色数组来跟踪用于绘制每个beizer路径的颜色,因为我可以选择改变每个路径的颜色。

- (void)drawRect:(CGRect)rect // (4)
{
    UIBezierPath *currentPath;
    UIColor *currentColor;
    for (int index=0;index<[self.pathArray count]; index++)
    {
        currentPath = [self.pathArray objectAtIndex:index];
        currentColor = [self.strokeArray objectAtIndex:index];
        [currentColor setStroke];
        [currentPath stroke];
    }
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];


    UIColor *currentStrokeColor;
    currentStrokeColor = [self copyColor:self.strokeColor];
    self.path = [UIBezierPath bezierPath];
    [self.path setLineWidth:2.0];
    [self.pathArray addObject:self.path];
    [self.strokeArray addObject:currentStrokeColor];
    [self.path moveToPoint:p];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [self.path addLineToPoint:p]; // (4)
    [self setNeedsDisplay];
}

1 个答案:

答案 0 :(得分:1)

绘制路径后,您可以将其渲染为图像。这样,每条路径都会连接成一个图像,从而对绘图效率更高。有关代码段,请参阅this question