Graphics Programs Reference
In-Depth Information
Detecting Taps with UITapGestureRecognizer
The first UIGestureRecognizer subclass you will use is UITapGestureRecog-
nizer . When the user taps a line in TouchTracker , you will present a menu that allows
them to delete it. Open TouchTracker.xcodeproj from Chapter 19 .
In the first part of this section, we are going to recognize a tap, determine which line is
close to where the tap occurred, store a reference to that line, and change that line's color to
green so that the user knows it has been selected.
In TouchDrawView.m , edit the initWithFrame: method to create an instance of
UITapGestureRecognizer and attach it to the TouchDrawView being initialized.
- (id)initWithFrame:(CGRect)r
{
self = [super initWithFrame:r];
if (self) {
linesInProcess = [[NSMutableDictionary alloc] init];
completeLines = [[NSMutableArray alloc] init];
[self setBackgroundColor:[UIColor whiteColor]];
[self setMultipleTouchEnabled:YES];
UITapGestureRecognizer *tapRecognizer =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap:)];
[self addGestureRecognizer:tapRecognizer];
}
return self;
}
Now whenever a tap occurs on this TouchDrawView , the UITapGestureRecog-
nizer will send it the message tap: . In TouchDrawView.m , implement the tap:
method. For now, just have it log something to the console.
- (void)tap:(UIGestureRecognizer *)gr
{
NSLog(@"Recognized tap");
}
Build and run the application and then tap on the screen. You should notice two things: the
console reports that a tap was recognized, and a dot is drawn on the view. That dot is a very
short line and a bug. In this application, we don't want anything drawn in response to a tap.
Add the following code to tap: to remove any lines in process and redisplay the view.
Search WWH ::




Custom Search