2014-05-06 24 views
0

我想提及这篇文章是类似于Continuous drawing in CGContext with drawRect,但它没有任何代码片段解决方案,所以再次询问。保持以前的UIBezierPath笔画在连续调用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]; 
} 
+0

您对此解决方案有任何问题吗? –

+0

不,但我认为这是一个有效的解决方案?就像每次我做一个新的笔画一样,我会清除草图并重新绘制所有的草图。这是否有效?这不会导致很多路径闪烁吗? –

+1

现在我明白你的意思了。确实有更好的方法。在每一次绘制线条之后,您可以将整个视图渲染为图像,以便之后将线条绘制在静态图像上方。看到这个问题:http://stackoverflow.com/questions/19362670/drawing-application-for-ios-performance-issue/19365625#19365625 –

回答

1

绘制一条路径后,您可以将其渲染为图像。通过这种方式,每条路径都可以连接成一个单一的图像,这对绘图来说效率会更高。代码片段请参阅this question