2013-04-07 45 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]; 

}