Andrés Zorrilla
Andrés Zorrilla

Reputation: 111

How I can to repeat a animation for buttons in a loop in Objective C?

I have a problem. I want to animate some buttons in a loop. It works it if the squence don't repeat, but I have a problem when the sequence repeat in the array.

- (void)flash:(id)sender delay:(int)time;
 {
     UIButton *button = (UIButton *)sender;     
     NSTimeInterval duration = 1;    
     [UIView animateWithDuration:duration
                           delay:time + 0.5
                         options:UIViewAnimationOptionAutoreverse
                      animations:^{
                          [button setAlpha:0.1f];
                      } completion:^(BOOL finished) {
                          [button setAlpha:1.0f]; 
                      }
      ];
 }

(this method works succesfully, where I call the method "flash")

- (void) play: (id)sender
{
    //buttons is a array that contains buttons => 
    //buttons = [right, left, up, down, nil]


    //positions is a sequence of the buttons positions
    //if buttons positions is [0,1,2,3]
    //positions have the random positions of buttons, example:
    //positions =  [0, 3, 4, 5,8, 7,9]


    int randomNumber;
    NSNumber *index; 

    randomNumber = (arc4random() % 4);
    [positions addObject: [NSNumber numberWithInteger:randomNumber]];
    UIButton *btn;    

    for(int i=0; i<[positions count]; i++)
    {
        index = [positions objectAtIndex:i];
        btn = [buttons objectAtIndex:[index intValue]];
        [self flash:btn delay:i];        



    }

}

if the sequence of positions don't repeat, for example: positions= [1,2,3,0], the problem don't have problem. But if the sequence is for example: positions = [0,0,0,1] (it repeats the numbers), the program don't show the animation.

Upvotes: 0

Views: 1531

Answers (2)

Ayush
Ayush

Reputation: 3999

This Method Will animate the View that you would pass to it.

-(void)flashingAnimation:(BOOL)boolVal forView:(UIView *) view{
[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionRepeat | 
 UIViewAnimationOptionAutoreverse
                 animations:^{
                     view.alpha = 0.0f;
                     view.frame = CGRectMake(view.frame.origin.x*0.75, view.frame.origin.y*0.75, view.frame.size.width*1.5, view.frame.size.height*1.5);
                 }   
                    completion:^(BOOL finished){
                     // Do nothing
                 }];
[UIView commitAnimations];
}

And Finally call the above method by passing the View you want to animate.

[self flashingAnimation:YES forView:Btn]

In Options add UIViewAnimationOptionRepeat

Upvotes: 1

Mundi
Mundi

Reputation: 80265

It does not work because when you are calling flash and the button you are sending is the same as that which is already being animated the animation is interrupted (also if it has not really started yet due to the delay).

Try to refactor to keep track of the timing separately and use animations without delay. Maybe one solution is a completion block that calls itself recursively.

Upvotes: 1

Related Questions