views:

259

answers:

3

What's the easiest/fastest/most efficient way to perform a gradual (0.5 sec) fade from Default.png to the initial app view?

My initial try, which doesn't work so well .. it's Saturday night, let's see if we can do better :)

UIImageView* whiteoutView = [[UIImageView alloc] initWithFrame:self.view.frame]; // dealloc this later ??
whiteoutView.image = [UIImage imageNamed:@"Default.png"];
whiteoutView.alpha = 1.0;
[self.view.frame addSubview:whiteoutView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:0.5];
whiteoutView.alpha = 0;
[UIView commitAnimations];
A: 

Other than using setAnimationDelay: instead of setAnimationDuration:, it looks pretty good. What don't you like about the results?

Edit: Wow beaten hard.

David Kanarek
+1  A: 

What about:

UIImageView* whiteoutView = [[[UIImageView alloc] initWithFrame:self.view.frame] autorelease];
if (whiteoutView != nil)
{
    whiteoutView.image = [UIImage imageNamed:@"Default.png"];
    [self.view addSubview:whiteoutView];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration: 0.5];
    whiteoutView.alpha = 0.0;
    [UIView commitAnimations];
}

(The things you had wrong were setAnimationDelay vs setAnimationDuration, not properly releasing the view and trying to add the view to self.view.frame instead of self.view. The compiler should have caught that last one. Did it?)

St3fan
Works, except in my case I have a UITabBarController and have to do `[self tabBarController].view` instead of `self.view`. Works!
sehugg
+1  A: 

Here's a simple view controller that fades out the default image and removes itself from the view hierarchy. The advantage to this approach is that you can use this without modifying your existing view controllers...

@interface LaunchImageTransitionController : UIViewController {}
@end
@implementation LaunchImageTransitionController

- (void)viewDidLoad {
  [super viewDidLoad];

  self.view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]] autorelease];
  [UIView beginAnimations:nil context:nil];
  [UIView setAnimationDuration:.5];
  [UIView setAnimationDelegate:self];
  [UIView setAnimationDidStopSelector:@selector(imageDidFadeOut:finished:context:)];
  self.view.alpha = 0.0;
  [UIView commitAnimations];

}
- (void)imageDidFadeOut:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
  [self.view removeFromSuperview];
  //NOTE: This controller will automatically be released sometime after its view is removed from it' superview...
}
@end

Here is how you might use it in your app delegate:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

  //create your root view controller, etc...
  UIViewController *rootController = ....

  LaunchImageTransitionController *launchImgController = [[[LaunchImageTransitionController alloc] init] autorelease];

  [window addSubview:rootController.view];
  [window addSubview:launchImgController.view];

  [window makeKeyAndVisible];
} 
joshrl