I want to develop an app to draw touching the iPhone screen an searching how to do it I have found an example recipe (09-Paint) into the chapter eight of the Erica Sadun iPhone Developers Cookbook 3.0 Code Samples. In the example, as you could see under this lines, the touches over the screen are stored into an NSMutableArray
to later be drawn using the UIView
method drawRect
.
With this solution I have a problem when the points
array keeps an important number of points. How can I solve this problem? How can I call the drawRect
method without clearing the UIView
and just adding the last line (between the already painted line and the last point of the points
array)?
// Start new array
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
{
self.points = [NSMutableArray array];
CGPoint pt = [[touches anyObject] locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:pt]];
}
// Add each point to array
- (void) touchesMoved:(NSSet *) touches withEvent:(UIEvent *) event
{
CGPoint pt = [[touches anyObject] locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:pt]];
[self setNeedsDisplay];
}
// Draw all points
- (void) drawRect: (CGRect) rect
{
if (!self.points) return;
if (self.points.count < 2) return;
CGContextRef context = UIGraphicsGetCurrentContext();
[current set];
CGContextSetLineWidth(context, 4.0f);
for (int i = 0; i < (self.points.count - 1); i++)
{
CGPoint pt1 = POINT(i);
CGPoint pt2 = POINT(i+1);
CGContextMoveToPoint(context, pt1.x, pt1.y);
CGContextAddLineToPoint(context, pt2.x, pt2.y);
CGContextStrokePath(context);
}
}
Thanks for reading.
Now it works right. SkylarEC has a great tutorial here, where he explains how to solve this problem. Thanks for reading.