Unable to run two OpenGL ES animations on two view controllers in an application
I was hoping for some help on this as I'm really stuck after trying to fix it for a few days.
Basically, my app has an OpenGL ES animation called levelsView that is displayed as soon as it has op开发者_开发技巧ened. Here is the code that starts my animation on the view controller:
- (void)animate
{
levelsView.animationInterval = 1.0 / 60.0;
[levelsView startAnimating];
[levelsView release];
}
The view controller also has a Switch button that lets the user change the animation. The code that displays the other animation is below:
- (IBAction) Switch: (id) sender {
SnowFallViewController* vce = [[[SnowFallViewController alloc] initWithNibName:@"SnowFallViewController"
bundle:nil] autorelease];
[self presentModalViewController:vce animated:YES];
}
Also here is the view for the code that starts the second animation on the second view controller:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect rect = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame:rect];
GLViewController *theController = [[GLViewController alloc] init];
self.controller = theController;
[theController release];
GLView *glView = [[GLView alloc] initWithFrame:rect];
[window addSubview:glView];
glView.controller = controller;
glView.animationInterval = 1.0 / kRenderingFrequency;
[glView startAnimation];
[glView release];
[window makeKeyAndVisible];
}
Basically, the problem I'm having is that I cannot get both the animations to work in the same application i.e. when the application is loaded up the first animation works but when the user clicks on the Switch button they only get a blank screen instead of the second animation.
One thing I've noticed is that the second animation will work if I do not start the first animation i.e. if I got in my animation code and delete [levelsView startAnimating]; then the second animation will work(but obviously the first one will not).
Anyone with any insight on how I can fix this so I can get both animations to work?
Thanks,
Dave
Assuming you're using a CADisplayLink
inside your GLView to run your animation loop, it seems from experience that they have mechanisms inside CADisplayLink to make sure that it doesn't just keep issuing calls upon calls if you're not keeping up with the refresh rate. It's more than possible that logic confuses itself a little if you have multiple CADisplayLinks attached at once.
I'd strongly suggest you add:
- (void)viewDidAppear:(BOOL)animated
{
[glView startAnimation];
}
- (void)viewWillDisappear:(BOOL)animated
{
[glView stopAnimation];
}
Those tie into the built-in mechanisms surrounding presenting and dismissing a view controller — the view controller is being told in the first instance when its view did appear (ie, the transition in is finished) and in the second when its view is about to disappear (ie, just before the transition outward begins). By stopping and starting animation on your GL view based on whether your controller is visible you'll save a lot of processing and prevent your disparate OpenGL views from fighting with each other for rendering times.
精彩评论