Game Development Reference
In-Depth Information
Listing 8-5. TouchEventsController.m (doubleTap:)
-(void)doubleTap:(UITapGestureRecognizer*)doubleTap{
float scale;
if ([doubleTap numberOfTouches] == 1){
scale=2.0;
} else {
scale=0.5;
}
NSLog(@"Touches: %i", [doubleTap numberOfTouches]);
for (Spark* spark in [self actorsOfType:[Spark class]]){
float radius=[spark radius]*scale;
if (radius<2){
radius=2;
} else if (radius>128){
radius=128;
}
[spark setRadius:radius];
}
}
In Listing 8-5, we check to see if the UITapGestureRecognizer doubleTap that triggered this task is
configured for a single touch. Based on this result of this check, we set the value of scale to either
2.0 or 0.5. In this way, a single-finger double tap will increase the scale of the Spark objects, and a
two-finger double tap will reduce the size of the Spark objects. Once we have the correct value for
scale, we simply iterate over all of the Spark objects in the scene and adjust their radius based on
scale.
We have looked at touch events so we can understand the touch event lifecycle. We also
took a quick look at the UITapGestureRecognizer class. The following section will discuss the
UITapGestureRecognizer in more detail.
Understanding Gesture Recognizers
iOS allows the user to interact with an application with a rich set of gestures. These include taps,
swipes, pinches, and so on. Each of these gestures comprises some reasonably complex logic to
interpret touch events into a cohesive gesture. If this was left up to each developer to implement,
the user experience would be pretty bad, as each developer would inevitably make different
assumptions about each gesture. For example, the long press gesture has a default duration. We
would want this consistent across all applications because we don't want to “retrain” each user for
each application as to what a long press is. In this spirit, Apple provides a number of classes that
are designed to identify the most common gestures a developer is going need. Each of the gesture
recognizers extends the abstract base class UIGestureRecognizer and is shown in Listing 8-6.
Listing 8-6. Concrete Implementations of UIGestureRecognizer
UITapGestureRecognizer
UIPinchGestureRecognizer
UIRotateGestureRecognizer
 
Search WWH ::




Custom Search