我有一个应用程序,按下时当前旋转按钮(图像)两次。我想循环缩放按钮并摇动按钮。所以这三个动画将是:缩放,平移,旋转。我怎样才能随机循环这些?这是我目前拥有的按钮:
- (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];
}
答案 0 :(得分: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];
}