Graphics Programs Reference
In-Depth Information
}
}
}
// If nothing is close enough to the tapped point, then we didn't select a line
return nil;
}
(There are better ways to implement lineAtPoint: , but this simplistic implementation
is okay for our purposes.)
The point we are interested in, of course, is where the tap occurred. We can easily get this
information. Every UIGestureRecognizer has a locationInView: method.
Sending this message to the gesture recognizer will give you the coordinate where the
gesture occurred in the coordinate system of the view that is passed as the argument.
In TouchDrawView.m , send the locationInView: message to the gesture recog-
nizer, pass the result to lineAtPoint: , and make the returned line the selec-
tedLine .
- (void)tap:(UIGestureRecognizer *)gr
{
NSLog(@"Recognized tap");
CGPoint point = [gr locationInView:self];
[self setSelectedLine:[self lineAtPoint:point]];
// If we just tapped, remove all lines in process so a new one
// isn't drawn on every tap
[linesInProcess removeAllObjects];
[self setNeedsDisplay];
}
Finally, in TouchDrawView.m , update drawRect: to draw the selectedLine in
green. Make sure this code comes after you draw the complete and in progress lines.
[[UIColor redColor] set];
for (NSValue *v in linesInProcess) {
Line *line = [linesInProcess objectForKey:v];
CGContextMoveToPoint(context, [line begin].x, [line begin].y);
CGContextAddLineToPoint(context, [line end].x, [line end].y);
CGContextStrokePath(context);
}
// If there is a selected line, draw it
if ([self selectedLine]) {
[[UIColor greenColor] set];
CGContextMoveToPoint(context, [[self selectedLine] begin].x,
[[self selectedLine] begin].y);
CGContextAddLineToPoint(context, [[self selectedLine] end].x,
[[self selectedLine] end].y);
CGContextStrokePath(context);
Search WWH ::




Custom Search