avinash
avinash

Reputation: 611

UIButton flip animation in for loop

My code is give below,

int i;
for (i=1; i < 13; i++ ){
    UIButton * myButton1 = (UIButton *)([self.view viewWithTag:i]);
    NSString *imageLoop2=[NSString stringWithFormat:@"%@",[arr1 objectAtIndex:i-1]];

    [UIView beginAnimations:@"Flip" context:NULL];
    [UIView setAnimationDuration:0.60];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myButton1 cache:NO];
    myButton1.userInteractionEnabled = NO;
    myButton1.alpha=1.0f;
    [myButton1 setBackgroundImage:[UIImage imageNamed:imageLoop2] forState:UIControlStateNormal];

    [UIView commitAnimations];

}

it flip all button in same time duration.
But my problem is it flip when one by one mens"first button complete flip animation then second button start flip"

please give me suggestion.

Thanx

Upvotes: 0

Views: 2493

Answers (4)

Bhavesh Nayi
Bhavesh Nayi

Reputation: 3656

int i;
[UIView beginAnimations:@"Flip" context:NULL];
[UIView setAnimationDelegate:self];
float delay = 0;
float duration = 0.6;

for (i=1; i < 13; i++ ){
    UIButton * myButton1 = (UIButton *)([self.view viewWithTag:i]);
    NSString *imageLoop2 = [NSString stringWithFormat:@"%@",[arr1 objectAtIndex:i-1]];

    [UIView setAnimationDelay:delay];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myButton1 cache:NO];
    myButton1.userInteractionEnabled = NO;
    myButton1.alpha=1.0f;
    [myButton1 setBackgroundImage:[UIImage imageNamed:imageLoop2] forState:UIControlStateNormal];
    delay = delay + duration;

}
[UIView commitAnimations];

Upvotes: 0

avinash
avinash

Reputation: 611

imageIndex++   somewhere globally.

self.view.userInteractionEnabled =NO;

imageIndex++;
if (imageIndex == 13){
    self.view.userInteractionEnabled =YES;
    imageIndex=0;
    return;
}

 UIButton * myButton1 = (UIButton *)([self.view viewWithTag:imageIndex]);
[UIView beginAnimations:@"Flip" context:NULL];
[UIView setAnimationDuration:0.40];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myButton1 cache:NO];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(stopButton)];


 NSString *imageLoop2 = [NSString stringWithFormat:@"%@",[arr1 objectAtIndex:imageIndex-1]];
 myButton1.userInteractionEnabled = NO;
 myButton1.alpha=1.0f;
 [myButton1 setBackgroundImage:[UIImage imageNamed:imageLoop2] forState:UIControlStateNormal];

 [UIView commitAnimations]; 

Upvotes: 1

EmptyStack
EmptyStack

Reputation: 51374

Try this. Declare i somewhere globally.

i = 0;
[self flipNextButton];

Define a flipNextButton method like this.

- (void)flipNextButton {

    i++;
    if (i == 13) return;

    [UIView beginAnimations:@"Flip" context:NULL];
    [UIView setAnimationDuration:0.60];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myButton1 cache:NO];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(flipNextButton);

    UIButton * myButton1 = (UIButton *)([self.view viewWithTag:i]);
    NSString *imageLoop2 = [NSString stringWithFormat:@"%@",[arr1 objectAtIndex:i-1]];
    myButton1.userInteractionEnabled = NO;
    myButton1.alpha=1.0f;
    [myButton1 setBackgroundImage:[UIImage imageNamed:imageLoop2] forState:UIControlStateNormal];

    [UIView commitAnimations];
}

Upvotes: 1

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73588

to do animation one after the other in the same block.Try [UIView setAnimationDelay:delay];

Also keep in mind the 2 block or single block animations are performed in background thread. So with this change -

int i;
[UIView beginAnimations:@"Flip" context:NULL];
[UIView setAnimationDelegate:self];
float delay = 0;
float duration = 0.6;

for (i=1; i < 13; i++ ){
    UIButton * myButton1 = (UIButton *)([self.view viewWithTag:i]);
    NSString *imageLoop2 = [NSString stringWithFormat:@"%@",[arr1 objectAtIndex:i-1]];

    [UIView setAnimationDelay:delay];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myButton1 cache:NO];
    myButton1.userInteractionEnabled = NO;
    myButton1.alpha=1.0f;
    [myButton1 setBackgroundImage:[UIImage imageNamed:imageLoop2] forState:UIControlStateNormal];
    delay = delay + duration;

}
[UIView commitAnimations];

Also Apple Docs say that the single block animations are more smoother in terms of performance compared to the 2 block animation you are doing. Just a thought...

Upvotes: 1

Related Questions