2013-07-12 62 views
0

我必須爲UIButton數組執行縮小和展開動畫。對於單一的UIButton我做這樣的...如何動畫(縮小和展開)到UIButton數組

UIButton *button = [self.destinationButtonsArray objectAtIndex:0]; 
[UIView beginAnimations:@"shrink" context:(__bridge void *)(button)]; 
    [UIView animateWithDuration:0.7f delay:0 options:UIViewAnimationOptionAutoreverse |    UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction animations:^{ 

     [UIView setAnimationRepeatCount:3]; 

     CGAffineTransform t = CGAffineTransformMakeScale(1.2f, 1.2f); 
     button.transform = t; 


    } completion:^(BOOL finished) { 
     button.transform = CGAffineTransformMakeScale(1.0f, 1.0f);}]; 

我怎樣才能實現UIbutons的陣列相同的效果。

+0

當您將代碼放入for循環時,您是否看到問題? – Wain

回答

-1

使用for動畫塊中,像這樣:

[UIView beginAnimations:@"shrink" context:(__bridge void *)(button)]; 
     [UIView animateWithDuration:0.7f delay:0 options:UIViewAnimationOptionAutoreverse |       UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat |  UIViewAnimationOptionAllowUserInteraction animations:^{ 

    [UIView setAnimationRepeatCount:3]; 

    CGAffineTransform t = CGAffineTransformMakeScale(1.2f, 1.2f); 

    for (UIButton button in self.destinationButtonsArray) { 
     button.transform = t; 
    } 
} completion:^(BOOL finished) { 
    for (UIButton button in self.destinationButtonsArray) { 
     button.transform = CGAffineTransformMakeScale(1.0f, 1.0f);}]; 
    } 
}]; 
+0

它對我來說工作得很好 –

1

您可以使用類。聲明一個UIButton類別並添加方法來執行動畫。

的UIButton + Transform.h

@interface UIButton (Transform) 

- (void) applyAnimation; 

@end 

的UIButton + Transform.m

@implementation UIButton (Transform) 

- (void) applyAnimation { 
    [UIView beginAnimations:@"shrink" context:self]; 
    [UIView animateWithDuration:0.7f 
          delay:0 
         options:UIViewAnimationOptionAutoreverse 
           | UIViewAnimationCurveEaseInOut 
           | UIViewAnimationOptionRepeat 
           | UIViewAnimationOptionAllowUserInteraction 
      animations:^{ 
        [UIView setAnimationRepeatCount:3]; 
        CGAffineTransform t = CGAffineTransformMakeScale(1.2f, 1.2f); 
        self.transform = t; 
      } 
      completion:^(BOOL finished) { 
       self.transform = CGAffineTransformMakeScale(1.0f, 1.0f); 
      } 
    ]; 
} 

@end 

呼叫如下

[self.destinationButtonsArray makeObjectsPerformSelector:@selector(applyAnimation)]; 

這將調用陣列上的方法animat離子方法在數組中的所有按鈕上。

希望有幫助!