0

We have seven buttons, on clicking of anyone among them, i need to perform animations on remaining buttons "in sequence". At present am using the following code in for loop,but animation is done on all the buttons at once.

 [UIView beginAnimations: @"Fade In" context:nil];
 button.alpha = 0;
 [UIView setAnimationDelay:0];
 [button setTitle:@"helloworld"  forState:UIControlStateNormal];
 [UIView setAnimationDuration:0];
 button.alpha = 1;
 [UIView commitAnimations]; 
Rakii
  • 75
  • 1
  • 10

2 Answers2

4

You should not use beginAnimations:context: any more. To quote Apple's docs:

Use of this method is discouraged in iOS 4.0 and later. You should use the block-based animation methods to specify your animations instead.

Use the new animateWithDuration family of methods instead. Specifically animateWithDuration:delay:options:animations:completion:. That method takes a delay parameter. You can issue a series of animation commands, each with a larger delay value. You can also chain animations by making the completion block of each animation trigger the next animation.

Edit: here is a link to a post where I show code that uses the completion method to trigger a sequence of animations one right after the other:

Scrolling the array of images using the CABasicAnimation

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Block-based UIView animations' completion blocks don't get called if interrupted for example user leaving the app etc. – durazno Apr 09 '16 at 12:23
1

Try this thing in for loop

for(int btnIndex = 0; btnIndex < 7; btnIndex++) {

  UIButton *button = [buttons objectAtIndex:0]; //get button reference from an array

  [UIView beginAnimations: @"Fade In" context:nil];
button.alpha = 0;
  [UIView setAnimationDelay:(0.1 * btnIndex)];
  [button setTitle:@"helloworld"  forState:UIControlStateNormal];
  [UIView setAnimationDuration:0.1];
button.alpha = 1;
  [UIView commitAnimations];
}
Mrunal
  • 13,982
  • 6
  • 52
  • 96