views:

446

answers:

3

I'm using some pretty standard code to flip 2 UIImageViews that are inside a small view. (I'm amazed it worked!)

But what if I had THREE UIImageViews inside a small view... and wanted to flip between all 3?

I thought I could just cut/paste 2 copies of my code... but I guess not.
When I try to flip 1>2.. and then 2>3... it just flips once... going directly from 1>3. What happened to 2????

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:myView cache:YES]; 
[image1 removeFromSuperview];    
[myView addSubview:image2];
[UIView commitAnimations];

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:myView cache:YES]; 
[image2 removeFromSuperview];    
[myView addSubview:image3]; 
[UIView commitAnimations];
A: 

You could set a 0.5 second delay on the start of the second animation.

Also, you may want to check out keyframe animations to do more advanced stuff like this.

gerry3
+1  A: 

The animations are not chained together like this. Basically, they are doing both animations at the same time. What you want is to create a new method for the second flip that will be called after the first one is done:

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)contextn {
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:myView cache:YES]; 
    [image2 removeFromSuperview];    
    [myView addSubview:image3]; 
    [UIView commitAnimations];
}

Then in your existing method, put this line:

[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

like so:

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:myView cache:YES];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[image1 removeFromSuperview];    
[myView addSubview:image2];
[UIView commitAnimations];

For more info, check the apple docs

coneybeare
Wow... ok... I'll try that. But I thought "commitAnimations" was the whole point of "do it now"... instead of "don't do it now... wait until I run ANOTHER flip... and then cancel the previous one, altogether... without warning".
Bonnie
> For more info, check the apple docs I've read as many THOUSANDS of pages of iPhone docs... that I can possibly stand. And I still missed the part about "commitAnimations doesn't really DO the animation now".
Bonnie
@comment 1 - It doesnt cancel it, they are happening at the same time. @comment 2 - The iPhone is single threaded by default. The only way you could get it to work while your method is still being run is to have these methods run in the background and call performSelectorOnMainThread(because all UI has to be on the main thread). But even then, the animations would only start a nanosecond earlier than before. CommitAnimations is not a synchronous call… meaning your app does not pause inbetween these two animations.
coneybeare
A: 

Jill,

In your second code block, do the following.

[UIView beginAnimations:nil context:NULL];

// This will cause your second animation block to wait 0.5 second, which will be 
// enough time for the second one to kick in and do it's thing.
[UIView setAnimationDelay:0.5];

[UIView setAnimationDuration:0.5]; 
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:myView cache:YES]; 
[image2 removeFromSuperview];    
[myView addSubview:image3]; 
[UIView commitAnimations];
bstahlhood