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

}
4

1 回答 1

1

如果您想对按钮应用随机动画,您可以拥有一个CABasicAnimations 数组并随机选择其中一个。

在某处初始化和配置动画并将它们添加到数组中(我将其设为属性)。

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

}
于 2013-04-08T00:05:19.363 回答