0

我的代码在下面给出,

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

}

它在同一时间内翻转所有按钮。
但我的问题是当一个接一个“第一个按钮完成翻转动画然后第二个按钮开始翻转”时它会翻转

请给我建议。

谢谢

4

4 回答 4

1

尝试这个。i在全球某处声明。

i = 0;
[self flipNextButton];

像这样定义一个FlipNextButton方法。

- (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];
}
于 2011-08-29T09:15:08.447 回答
1

在同一个块中一个接一个地做动画。尝试[UIView setAnimationDelay:delay];

还要记住 2 块或单块动画是在后台线程中执行的。所以有了这个变化 -

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

Apple Docs 还说,与您正在执行的 2 块动画相比,单块动画在性能方面更流畅。只是一个想法...

于 2011-08-29T09:18:13.473 回答
1

imageIndex++ 全局某处。

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]; 
于 2011-08-30T04:10:42.780 回答
0
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];
于 2013-04-16T08:49:09.247 回答