ios - 在连续调用 drawRect 时保留之前的 UIBezierPath 笔划

标签 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];
}

最佳答案

绘制路径后,您可以将其渲染为图像。这样每条路径都被连接成一个图像,这对于绘图来说效率更高。请看this question用于代码片段。

关于ios - 在连续调用 drawRect 时保留之前的 UIBezierPath 笔划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23493040/

相关文章:

ios - UIBezierPath绘图占用CPU 100%

ios - 如何在 UIDatePicker 中禁用 24 小时格式

UIImage的ressizedImageWithCapInsets在iOS 7下带来多个CoreGraphics "invalid context 0x0"错误

ios - 用两个 CALayer 叠加绘制

ios - 将 UIImage 绘制为一个没有离屏渲染的圆圈

macos - CGFontGetGlyphWithGlyphName() 出现问题

iOS 碰撞检测

ios - 有充分的理由不让所有 iPhone/iPad 图形都只有视网膜尺寸吗?

ios - 核心图——用作趋势图的散点图

ios - NSFetchedResultsController 与项目在多个部分?