我想提一下,这篇文章与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];
}