Reputation: 7173
I'm exploring making a doodle app where the user draws pictures with their finger and I've come across several different ways of drawing lines to the screen. I've seen code anywhere from:
- (void)drawRect:(CGRect)rect // (5)
{
[[UIColor blackColor] setStroke];
[path stroke];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path moveToPoint:p];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path addLineToPoint:p]; // (4)
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
to:
mouseSwiped = YES; UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempDrawImage setAlpha:opacity];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
and the last method (one I came up with that makes the most sense for me at least)
- (void) viewDidLoad{
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(drawImage:)];
[self.view addGestureRecognizer:pan];
pan.delegate = self;
paths = [[NSMutableArray alloc]init];
}
- (void) drawImage:(UIPanGestureRecognizer*)pan{
CGPoint point = [pan translationInView:self.view];
[paths addObject:[NSValue valueWithCGPoint:point]];
}
In the last implementation, I would store the points the user is dragging along and draw a line as they draw. I feel like that would be kind of intensive with a lot of overhead though since there's a lot of drawing going on while the user is interacting with the app.
So my question is, is there a best practice/best way to do drawing? Does Apple prefer a particular way over the other and what are the advantages/disadvantages of doing it each way?
Upvotes: 0
Views: 684
Reputation: 452
A better way is to use the users touch points as points on a UIBezierPath
. See session 233 WWDC 2012... http://developer.apple.com/itunes/?destination=adc.apple.com.16351493766
Upvotes: 2