Reputation: 385
I have 3 UILabels
that I want to fade out, one after the other after a couple of seconds. My problem is these are happening all at once. I am trying to chain the animations, but I cannot get this to work. I've tried all sorts of suggestions but to no avail. I know it cannot be this hard. I preferably would like to bundle these together in one animation method, because I would like to trigger other functionality from the animationDidStop
after all 3 labels have been displayed. Any help or suggestions??
Here is my code:
- (void)viewDidLoad
{
[self fadeAnimation:@"fadeAnimation" finished:YES target:lblReady];
[self fadeAnimation:@"fadeAnimation" finished:YES target:lblSet];
[self fadeAnimation:@"fadeAnimation" finished:YES target:lblGo];
}
- (void)fadeAnimation:(NSString *)animationID finished:(BOOL)finished target:(UIView *)target
{
[UIView beginAnimations:nil context:nil];
[UIView beginAnimations:animationID context:(__bridge void *)(target)];
[UIView setAnimationDuration:2];
[target setAlpha:0.0f];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
Upvotes: 4
Views: 1581
Reputation: 5312
You should have them performSelector:withObject:afterDelay: instead.
So change your code to:
- (void)viewDidLoad
{
[self performSelector:@selector(fadeAnimation:) withObject:lblReady afterDelay:0];
[self performSelector:@selector(fadeAnimation:) withObject:lblSet afterDelay:2];
[self performSelector:@selector(fadeAnimation:) withObject:lblGo afterDelay:4];
}
-(void)fadeAnimation:(UIView *)target {
[self fadeAnimation:@"fadeAnimation finished:YES target:target];
}
- (void)fadeAnimation:(NSString *)animationID finished:(BOOL)finished target:(UIView *)target
{
[UIView beginAnimations:nil context:nil];
[UIView beginAnimations:animationID context:(__bridge void *)(target)];
[UIView setAnimationDuration:2];
[target setAlpha:0.0f];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
This just calls each action code after 0, 2, or 4 sections. If the animation duration is changed, those numbers should be changed correspondingly.
If you want to use block animations instead of using the old animation style, you could instead do:
[UIView animateWithDuration:2 animations ^{
//put your animations in here
} completion ^(BOOL finished) {
//put anything that happens after animations are done in here.
//could be another animation block to chain animations
}];
Upvotes: 1
Reputation: 318944
This would be easier with the latest UIView
animation methods:
[UIView animateWithDuration:2.0 animations:^ {
lblReady.alpha = 0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:2.0 animations:^ {
lblSet.alpha = 0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:2.0 animations:^ {
lblGo.alpha = 0;
} completion:^(BOOL finished) {
// Add your final post-animation code here
}];
}];
}];
Upvotes: 8