I want to develop an application to touch the screen of an iPhone to find how to do it. I found an example recipe (09-Paint) in the eighth chapter of the iPhone developers cookbook Erica Sadun 3.0 Code Samples . In the example, as you can see under these lines, the strokes on the screen are saved in NSMutableArray, so that later they can be drawn using the method UIView drawRect.
I have a problem with this solution when the array pointsstores a large number of points. How can I solve this problem? How can I call a method drawRectwithout clearing UIViewand just adding the last line (between the already drawn line and the last point of the array points)?
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
{
self.points = [NSMutableArray array];
CGPoint pt = [[touches anyObject] locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:pt]];
}
- (void) touchesMoved:(NSSet *) touches withEvent:(UIEvent *) event
{
CGPoint pt = [[touches anyObject] locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:pt]];
[self setNeedsDisplay];
}
- (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.