I have this code:
when image change alpha from 0.00 to 1.00 it is imediately and not in 3 seconds, why?
- (void) 开发者_如何学GostartAnimation{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[successView setAlpha:1.00];
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[successView setAlpha:0.00];
[UIView commitAnimations];}
Both animations are run in the same run cycle (that's how UIView animations work—all animations in one run cycle are "concatenated." You'll need to use
[UIView setAnimationDelay:3.0];
On the second animation.
Example Code
There are two ways to do this—using the standard begin/commit animations, or using the blocks method. This example uses the begin/commit animations code, which is what you have. The issue is because the animations are being concatenated, and without going into CA, the standard animation behavior is for one to be "forced" to end before the other runs. It has to do with run loops; Building Animation-Driven Interfaces from WWDC 2010 goes into depth about this. But the code to do what you want looks like this:
- (void)fadeOut {
[UIView beginAnimations:@"Animation2" context:NULL];
[UIView setAnimationDuration:3];
[self.testView setAlpha:0.00];
[UIView commitAnimations];
}
- (IBAction)animate {
[UIView beginAnimations:@"Animation1" context:NULL];
[UIView setAnimationDuration:3];
[self.testView setAlpha:1.00];
[UIView commitAnimations];
[self performSelector:@selector(fadeOut) withObject:nil afterDelay:3.0];
}
You have to force it to break up the run loop, basically. The key thing to remember is that animations are not executed sequentially, as you might expect code to be.
The blocks-based code looks like this. Note that I'm using the autorepeat option, which automatically repeats the animation. Note though that in the animation, you are setting a property, so by default after the animation completes the view will go back to being visible. Therefore, you have the set the property again to zero in the completion block.
- (IBAction)animate {
[UIView transitionWithView:self.testView duration:3.0 options:UIViewAnimationOptionAutoreverse animations:^{self.testView.alpha = 1.0;} completion:^(BOOL finished) {self.testView.alpha = 0.0;}];
}
Hopefully this helps!
精彩评论