Search code examples
ioscore-animation

why the animation in the didFinishLaunchingWithOptions failed?


I just want to diaplay the view with animation in the didFinishLaunchingWithOptions, my codes are like the following:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    CGRect finalRect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);

    UIImage *image = [UIImage imageNamed:@"122.jpg"]; 

    UIButton* pic = [[UIButton alloc] initWithFrame:finalRect];
    [pic setBackgroundImage:image forState:UIControlStateNormal];
    [pic setHidden:true];
    pic.center = CGPointMake(finalRect.size.width / 2, finalRect.size.height / 2);


    UIViewController* controller = [[UIViewController alloc] init];
    [controller setView:pic];

    [self.window setRootViewController:controller];
    [self.window makeKeyAndVisible];


    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.5];
    [pic setHidden:false];
    [UIView commitAnimations];

    return YES;
}

but the animation does not work at all. The subview with a picture just showed in the screen suddenly. But if I use a button to trigger the animation codes, the view will appear with animation as supposed to. Is there any restricts on the didFinishLaunchingWithOptions?

PS:

This problem is resolved by moving the animation codes into the viewDidAppear proc of the controller just like rokjarc said below.

But there is another solution for this by execute the animation codes in a delayed routine called from the didFinishLaunchingWithOptions like:

[self performSelector:@selector(executeAnimation) withObject:nil afterDelay:1];

Solution

  • Your button gets covered by controller.view which is also loaded in window.view on top of your pic.

    You can solve this by moving the code to controller::viewDidAppear.

    If you decide to go this way don't forget to add the button to controller.view instead of appDelegate.window.

    Since it looks that you only want to show this animation at application launch you could set add a BOOL property called showAnimation to controller. Set this property to YES in didFinishLaunchingWithOptions: and to NO at the end of controller::viewDidAppear.

    This way you can conditionally (if (self.showAnimation)...) show the desired animation only once (in controller::viewDidAppear).