开发者

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];
    [UIVie开发者_开发问答w 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


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...


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];
}


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]; 


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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜