I have this code, where successview start with alpha = 0.00
- (void) startAnimation{
//immediately
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[successView setAlpha:1.00];
[UIView commitAnimations];
//in three seconds
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[successView开发者_运维百科 setAlpha:0.00];
[UIView commitAnimations];
}
in this way, in first animation (alpha 0.00 to 1.00), it don't happen in 3 seconds but immediately, instead in the second animation (alpha 1.00 to alpha 0.00) it happens in 3 seconds
if I write only firts animation:
- (void) startAnimation{
//in three seconds
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[successView setAlpha:1.00];
[UIView commitAnimations];
}
it happens in 3 seconds, why in the forst example it don't happen?
- (void)startAnimation
{
[UIView beginAnimations:@"successAnimationPart1" context:nil];
[UIView setAnimationDuration:3];
[UIView setAnimationDelegate:self];
[successView setAlpha:1.00];
[UIView commitAnimations];
}
And add this animation delegate method:
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
if ([animationID isEqualToString:@"successAnimationPart1"]) {
// when first animation is done, call second
[UIView beginAnimations:@"successAnimationPart2" context:nil];
[UIView setAnimationDuration:3];
[UIView setAnimationDelegate:self];
[successView setAlpha:0.00];
[UIView commitAnimations];
}
}
You can try to set animation delay for your 2nd animation (note that using this api is discouraged and you should use block-based animateWithDuration:delay:options:animations:completion:
animation if you're targeting iOS4.0+):
...
[UIView setAnimationDelay:3.0];
...
But if you just want to change view's alpha to 1 and revert it back you can use just your 1st animation but make it autoreversing:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[UIView setAnimationRepeatAutoreverses:YES];
[successView setAlpha:1.00];
[UIView commitAnimations];
精彩评论