Game Development Reference
In-Depth Information
Starting at the upper left of Figure 4-4 , the High Score view flips from left to right until it becomes
just a sliver. The animation continues, revealing the Welcome view in a smooth, visually appealing
transition. This is just one of the many built-in animations in iOS. Users have learned that this
standard transition indicates a change in context. To understand how this animation was triggered,
let's start by looking at the steps preformed in Chapter 3 to switch from the High Score view to the
Welcome view. These steps are shown in Listing 4-4.
Listing 4-4. GameController.m (highscoreDoneClicked:)
- (IBAction)highscoreDoneClicked:(id)sender {
[UIView beginAnimations:@"AnimationId" context:@"flipTransitionToBack"];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.view cache:YES];
[highscoreController.view removeFromSuperview];
[self.view addSubview: welcomeView];
[UIView commitAnimations];
}
Listing 4-4 shows the task called when the user touches the Done button in the High Score view.
The first line of this task calls the static task beginAnimations:context: . This task is sort of like
starting a database transaction, in that it is creating an animation object, and any change that occurs
to UIView instances before commitAnimations is called is part of this animation. The first NSString
passed to beginAnimations:context: is an identifier for the backing animation object. The second
NSString is similar to the first in that it is used to give additional context about when the animation
was created. These two strings could be nil in this example, because we don't really care much
about this animation after we define it. In the next example, we will take advantage of these strings
to define code to be run when the animation is done.
After the call to beginAnimations:context: , the duration of the animation is defined by
calling setAnimationDuration: . The value passed in is the number of seconds the animation
should run. The call to setAnimationTransition:forView:cache: indicates that we want to
use one of the canned animations that comes with iOS. The animation we have selected is
UIViewAnimationFlipFromLeftForView . Because this is a transition animation, we specify the view
self.view , which is the super view whose content is changing.
After the animation is set up, the next thing we do (as shown in Listing 4-4) is make the changes to
our scene that we want animated. Because this task is responsible for switching the displayed view
from the High Score view to the Welcome view, we simply remove the view highscoreController.
view from its super view ( self.vew ) and add the welcomeView to self.view . Finally, we commit the
animation, causing it to be displayed to the user.
We can use these static tasks of UIView to create other animations, not just the ones provided by
iOS. Let's update the transition from the Welcome screen to the High Score screen so one fades into
the other, as seen in Figure 4-5 .
 
Search WWH ::




Custom Search