Game Development Reference
In-Depth Information
float scale = [pinchRecognizer scale];
float velocity = [pinchRecognizer velocity];
NSLog(@"Scale: %f Velocty: %f",scale, velocity);
float radius=startRadius*scale;
if (radius<8){
radius = 8;
} else if (radius>128.0){
radius=128;
}
[saucer setRadius:radius];
} else if (pinchRecognizer.state == UIGestureRecognizerStateEnded){
[saucer setAnimationPaused:NO];
} else if (pinchRecognizer.state == UIGestureRecognizerStateCancelled){
[saucer setRadius:startRadius];
[saucer setAnimationPaused:NO];
}
pinchGesture : that is called whenever a pinch gesture is detected
UIView actorsView . In this example, we are checking the state of the gesture recognizer. The
UIGestureRecognizerStateBegan ), is in
UIGestureRecognizerStateChanged ), or has terminated ( UIGestureRecognizerStateEnded
).
When the gesture starts, we record the radius of saucer and store it in the variable startRadius .
We also call setAnimationPaused : on saucer to stop if from animating while being scaled. It is not a
requirement to stop the animation. We are simply doing this so we can observe the change of the
gesture recognizer's state as we play with the sample on our phone.
When the gesture is in progress, we get the scale and velocity of the gesture. The scale value is the
change in scale since the beginning of the gesture. To put it another way, the scale is accumulative,
and that's why we apply the scale to the startRadius and not to the current radius of saucer. Once
we calculate our new radius, we apply that to saucer. The velocity is the scale factor per second.
This value can be used to calculate how much drift to apply to an animation after the gesture is over.
We are not using the value except to log it.
When the gesture has ended, normally we simply set the animationPaused property of saucer to No.
This starts the saucer animating again. If the gesture was cancelled, we reset the saucer back to its
original radius and unpause the animation. To test the cancel state, start with the saucer scaled all
the way down, then begin scaling it up with one hand while the other hand hits the power button at
the top of the iPhone. This will kick you out of the application. Then re-launch the sample application
and notice that the saucer is back at its original small scale.
The pinch gesture is the first multi-state gesture we have looked at in this chapter. The next gesture
we are going to look at is the pan or drag gesture, which is another multi-state gesture. We will see
that it behaves in very much the same way as the pinch gesture.
 
Search WWH ::




Custom Search