Iphone sequential animation with setAnimationDelay
I'm trying to chain animation events. The application I'm coding for work has a multiple choice quiz. First you pick your multiple choice answer. The quiz view fades away. Then a label ("correct" or "incorrect") fades in and then fades out. Finally the quiz fades back in again. The events are called and handled by a main viewController. I know I can chain events with the setAnimationDelegate and setAnimationDidStopSelector but I think it'll be easier and more clear to simply use setAnimationDelay so that all animations have time to finish before the next one fires.
I made this function that exists in the class that holds the "correct" "incorrect" label.
- (void)showIncorrect:(float)duration withDelay:(float)delay{
labelView.text = @"Incorrect!";
labelView.textColor = [UIColor redColor];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:delay];
[UIView setAnimationDuration:(duration/2)];
self.alpha = 1.0;
[UI开发者_JAVA技巧View commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:(duration/2+delay)];
[UIView setAnimationDuration:(duration/2)];
self.alpha = 0.0;
[UIView commitAnimations];
}
Can someone tell my why the first animation block is ignored? I've tried to nest one animation block into the other and this does not work either.
When you animate this way, you're actually setting the relevant value immediately, and telling what's onscreen to animate to catch up to that new reality. So whatever value you set in the first animation block is being effectively ignored, because you've overwritten the fact that alpha == 1 by the fact that alpha == 0.
For doing more complex animations, it is worth building and adding the animations using CoreAnimation. It is a little more work, but you get lots more control.
In this case, you want to use an instance of CAKeyframeAnimation. You'll probably also want to use a CABasicAnimation to animate in your new quiz view. You can use a CAAnimationGroup to make sure that multiple animations that you add are synchronized.
I've hit similar issues, what seems to happen is the iPhone is optimizing the drawing which occurs in the same function.
What you can try doing is splitting the function up and performSelector. For example
[self performSelector:@selector(hideMessage) withObject:self afterDelay:1.0];
I've used this a few times with good results. If you want to see an excellent write up on doing great game style effects check out the Formic example code in iPhone cool projects. Wolfgang really pushes views for very pleasing game style effects.
Google books has it, but the book overall has some really great techniques.
Heres the google book link: http://books.google.com/books?id=_9qtPLmQxf8C&pg=PA6&lpg=PA6&dq=wolfgang+ante+formic&source=bl&ots=-vxvV3oLap&sig=G0BLW1RpItws-fwmXDI3mIjje4U&hl=en&ei=tVnzSt-3MImEMpL_8OgF&sa=X&oi=book_result&ct=result&resnum=1&ved=0CAoQ6AEwAA#v=onepage&q=wolfgang%20ante%20formic&f=false
Rich
精彩评论