2013-04-07 43 views
0

我有一個應用程序,當按下按鈕(圖像)時會旋轉兩次。我想循環縮放按鈕並搖動按鈕。所以這三個動畫將是:縮放,平移,旋轉。我怎樣才能隨機循環?這是我目前擁有的按鈕:通過按鈕上的動畫隨機循環

- (IBAction)playAudioAction:(id)sender { 
    UIButton *btn=(UIButton *)sender; 

    CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
    fullRotation.fromValue = [NSNumber numberWithFloat:0]; 
    fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)]; 
    fullRotation.duration = 0.5; 
    fullRotation.repeatCount = 2; 
    [btn.layer addAnimation:fullRotation forKey:@"360"]; 

    [self playAudioOfType:btn.tag]; 

} 

回答

1

如果你想申請一個隨機動畫的按鈕,你可以有CABasicAnimation秒的數組,並隨機挑選其中之一。

某處初始化和配置動畫並將它們添加到數組(我會讓它成爲一個屬性)。

CABasicAnimation * fullRotation = ... 
CABasicAnimation * scale = ... 
CABasicAnimation * translate = ... 
self.animations = @[ fullRotation, scale, translate ]; 

然後,當你隨機選擇一個,刪除所有以前的並添加新的。

- (IBAction)playAudioAction:(id)sender { 
    UIButton *btn=(UIButton *)sender; 

    NSInteger randomIndex = arc4random_uniform(self.animations.count); 
    CABasicAnimation *randomAnimation = self.animations[randomIndex]; 
    [btn.layer removeAllAnimations]; 
    [btn.layer addAnimation:randomAnimation forKey:@"animation"]; 

    [self playAudioOfType:btn.tag]; 

}