Graphics Programs Reference
In-Depth Information
Next, in TouchDrawView.m , override touchesMoved:withEvent: to update the
end point of the line associated with the moving touch.
- (void)touchesMoved:(NSSet *)touches
withEvent:(UIEvent *)event
{
// Update linesInProcess with moved touches
for (UITouch *t in touches) {
NSValue *key = [NSValue valueWithNonretainedObject:t];
// Find the line for this touch
Line *line = [linesInProcess objectForKey:key];
// Update the line
CGPoint loc = [t locationInView:self];
[line setEnd:loc];
}
// Redraw
[self setNeedsDisplay];
}
When a touch ends, you need to finalize the line. However, a touch can end for two reas-
ons: the user lifts the finger off the screen ( touchesEnded:withEvent: ) or the op-
erating system interrupts your application ( touchesCancelled:withEvent: ). A
phone call, for example, will interrupt your application.
In many applications, you'll want to handle these two events differently. However, for
TouchTracker , you will write one method to handle both cases. Declare a new method in
TouchDrawView.h .
- (void)endTouches:(NSSet *)touches;
In TouchDrawView.m , implement endTouches: .
- (void)endTouches:(NSSet *)touches
{
// Remove ending touches from dictionary
for (UITouch *t in touches) {
NSValue *key = [NSValue valueWithNonretainedObject:t];
Line *line = [linesInProcess objectForKey:key];
// If this is a double tap, 'line' will be nil,
// so make sure not to add it to the array
if (line) {
[completeLines addObject:line];
[linesInProcess removeObjectForKey:key];
}
}
// Redraw
[self setNeedsDisplay];
}
Search WWH ::




Custom Search