At times, you want to add information to
your screen that overlays your view but does not of itself do anything.
For example, you might show a top scores list or some instructions or
provide a context-sensitive tooltip. Recipe 1 demonstrates how to use a UIView
animation block to fade a view into and out of sight. This recipe
follows the most basic animation approach. It creates a surrounding
view animation block and then adds the single line of code that sets
the alpha property.
One thing this
recipe does not do is wait for the animation to finish. The change in
the bar button item gets called as soon as the animations are
committed, nearly a second before they end. If you tap the Fade In/Fade
Out button quickly (you may want to slow the animation duration to see
this better), you discover that the new animation starts up, replacing
the old one, creating a visual discontinuity.
To address this, you might want to add a call to UIView with setAnimationBeginsFromCurrentState:, setting the argument to YES. This tells the iPhone to use the current state of the ongoing animation to start the next animation, avoiding that jump.
Recipe 1. Animating Transparency Changes to a View’s Alpha Property
@implementation TestBedViewController
- (void) fadeOut: (id) sender
{
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[[self.view viewWithTag:999] setAlpha:0.0f];
[UIView commitAnimations];
self.navigationItem.rightBarButtonItem =
BARBUTTON(@"Fade In",@selector(fadeIn:));
}
- (void) fadeIn: (id) sender
{
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[[self.view viewWithTag:999] setAlpha:1.0f];
[UIView commitAnimations];
self.navigationItem.rightBarButtonItem =
BARBUTTON(@"Fade Out",@selector(fadeOut:));
}
- (void) viewDidLoad
{
self.navigationItem.rightBarButtonItem =
BARBUTTON(@"Fade Out",@selector(fadeOut:));
}
@end