2011-08-29 19 views
0

我的代碼如下給予,UIButton的翻轉動畫在循環

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

} 

它翻動同一時間段的所有按鈕。
但我的問題是它翻轉時,一個男士「第一個按鈕完成翻轉動畫,然後第二個按鈕開始翻轉」

請給我建議。

Thanx

+0

你希望所有的按鈕在同一時間翻轉? – EmptyStack

+0

我想第一個按鈕完成翻轉動畫,然後下一個按鈕開始翻轉動畫 – avinash

回答

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

做動畫一前一後在相同block.Try [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]; 

也是蘋果文件說,單塊動畫在性能方面更加平滑相比,你正在做2塊動畫。只是一個想法...

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