IOs中的按钮旋转
我试图在一个应用程序中实现“旋转轮子”的效果,我可以让瓶子旋转,但它会停在相同的位置。我怎样才能让它随机停止?谢谢大家 代码如下:IOs中的按钮旋转,ios,random,cabasicanimation,Ios,Random,Cabasicanimation,我试图在一个应用程序中实现“旋转轮子”的效果,我可以让瓶子旋转,但它会停在相同的位置。我怎样才能让它随机停止?谢谢大家 代码如下: - (IBAction)spin:(id)sender { CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath: @"transform.rotation"]; fullRotation.fromValue = [NSNumber numberWithFloa
- (IBAction)spin:(id)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;
[button.layer addAnimation:fullRotation forKey:@"360"];
}
CABasicAnimation的toValue属性控制动画的最终位置。您当前正在将其设置为一个常量值(360*M_PI/180)。您可能需要使用arc4random()创建一个随机值,并将toValue设置为该值 例如:
float randomRadians = arc4random() / ((pow(2, 32)-1)) * M_PI*2;
fullRotation.toValue = [NSNumber numberWithFloat:randomRadians];
随机值的代码来自此答案:
注意:我尚未测试此解决方案。非常感谢!它起作用了,但我用了另一种方式。类似这样的东西-(iAction)btnRotate:(id)发送方{[UIView animateWithDuration:0.7 animations:^{imgRotate.transform=CGAffineTransformMakeRotation(arc4random()/((pow(2,2)-0.2))*M_PI*400;}];