Game Development Reference
In-Depth Information
for (int touch=1;touch<=4;touch++){
UITapGestureRecognizer* tripleTap=[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tapGesture:)];
[tripleTap setNumberOfTapsRequired:3];
[tripleTap setNumberOfTouchesRequired:touch];
UITapGestureRecognizer* doubleTap=[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tapGesture:)];
[doubleTap setNumberOfTapsRequired:2];
[doubleTap setNumberOfTouchesRequired:touch];
[doubleTap requireGestureRecognizerToFail:tripleTap];
UITapGestureRecognizer* singleTap=[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tapGesture:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:touch];
[singleTap requireGestureRecognizerToFail:doubleTap];
[actorsView addGestureRecognizer:tripleTap];
[actorsView addGestureRecognizer:doubleTap];
[actorsView addGestureRecognizer:singleTap];
}
return YES;
}
return NO;
}
In Listing 8-9, we see the task
doSetup
for the class
TapGestureController
. In this task,
we generate the 12
Powerup
objects and add them as actors. We also keep track of each
Powerup
in the
NSMutableArray powerups
. After creating the
Powerup
objects, we create
12
UITapGestureRecognizers
—done for each
Powerup
. Each
UITapGestureRecognizer
is
added to
actorsView
with the task
addGestureRecognizer
:. Note that we are creating a
UITapGestureRecognizer
for each possible combination of taps and touches, instead of trying to
use a single
UITapGestureRecognizer
to recognize the various combinations. Although it is possible
in some cases to do this, everything works a lot better when you are as concise as possible with
UIGestureRecognizers
, as there are some subtle interactions between gesture recognizers. For
example, the recognizer responsible for detecting triple taps has to cancel those responsible for
double and single taps.
Each
UITapGestureRecognizer
is configured to call
tapGesture
: when a gesture is recognized, as
shown in Listing 8-10.
Listing 8-10. TapGestureController.m (tapGesture:)
- (void)tapGesture:(UITapGestureRecognizer *)sender{
int taps=[sender numberOfTapsRequired];
int touches=[sender numberOfTouches];
int index=(taps-1)*4+(touches-1);