Reputation: 11
I am trying to get the code correct to play an opening animation on my iphone app I created in xcode. I want it to only play when the app launches (like angry birds) not every time I go back to the main page. I have tried several things and nothing seems to be working correctly. Can someone tell me how to correctly do this?
I am using a series of images to create the animation.
I have tried to put the animation code from geeky lemon tutorial (http://www.youtube.com/watch?v=5AbdZ-8JBZQ) in the viewDidLoad. This does work and play the animation but every time I go back to the main page the animation plays. I don't want the animation to play when go back to the main page, just when the app starts.
I have also tried to add another view that is the first view when the app launches and have the animation play in viewDidLoad there and then with perform selector go to the main page after the animation is complete (done with the after Delay part). This also works and does play the animation and does not play when I go back to the main screen BUT this is causing some error making the app crash. I have put NSLogs in the viewDidLoad sections so I can see the pages are working and sometimes when I am in the app playing it says that my opening animation viewDidLoad is being called. So I think this has something to do with the crashing. When I take out this view and go directly to the main view it doesn't crash.
I have tried to put it in the app delegate also and I can't get that to work at all.
Please help this is driving me crazy I can't figure out what is going wrong. I know it can work angry birds and others do this very successfully.
If there is a better method?
Upvotes: 1
Views: 5641
Reputation: 2950
Try this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window makeKeyAndVisible];
UIImageView *imageView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"myImage1.png"], [UIImage imageNamed:@"myImage2.png"], [UIImage imageNamed:@"myImage3.png"], [UIImage imageNamed:@"myImage4.png"], [UIImage imageNamed:@"myImage5.png"], [UIImage imageNamed:@"myImage6.png"], nil];
imageView.animationDuration = 0.5;
imageView.animationRepeatCount = 3;
[imageView startAnimating];
[self.window addSubview:imageView];
return YES;
}
add the animation as a subview for you self.window
Upvotes: 6
Reputation: 14841
Try putting the animation code in your viewDidAppear using dispatch_once. That way, you will ensure that the animation will be called just once during the lifetime of the application.
Upvotes: 0
Reputation: 3054
Or you can also put it in
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
I have a view controller and in that I load the view. Call this from there.
If your animation is in a single view you can have that animation running on a separate thread with sleep on the main thread.
(void)applicationDidFinishLaunching:(UIApplication *)application {
// Add sub view[window addSubview:animationController.view];
// after some time do another thing
[self performSelector:@selector(firstcontroller) withObject:nil afterDelay:5];
[window makeKeyAndVisible]; }-(void)firstcontroller{
[window addSubview:firstControllerAfterLaunch.view];
}
Upvotes: 0
Reputation:
Put your animation method into the
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)opts
method. Use a separate view designated for the animation. Add it to the key window of the application and remove it when the animation ends.
Upvotes: 0
Reputation: 450
You will want to include it in the - (void)applicationWillEnterForeground:(UIApplication *)application method of your app delegate, this is called only when your app is "launched".
Upvotes: 0