サイコロのアクションが円で表示されるゲームを作成しています。ユーザーがホイールをクリックすると、回転してランダムな位置で停止します。次のように実装しようとしたコードを検索して見つけました:-
[UIView beginAnimations:@"RotationAnimation" context:nil];
CALayer *myLayer = diceCircle.layer;
CABasicAnimation *fullRotationAnimation;
fullRotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotationAnimation .fromValue = [NSNumber numberWithFloat:0];
// fullRotationAnimation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
fullRotationAnimation.toValue = [NSNumber numberWithFloat:angle];
fullRotationAnimation.duration = 0.5; // speed for the rotation. Smaller number is faster
fullRotationAnimation.repeatCount = 1; // number of times to spin. 1 = once
[myLayer addAnimation:fullRotationAnimation forKey:@"360"];
[UIView commitAnimations];
角度は次から来ています:-
-(IBAction)diceButtonClicked:(id)sender
{
float angle=arc4random()%360;
NSLog(@"%f",angle);
float rad=radians(angle);
NSLog(@"%f",rad);
[self rotateDiceMethod:rad];
}
しかし、ホイールは毎回同じ位置で停止し、常にアニメーション化するとは限りません。必要な機能を実装するためのアプローチまたはサンプル コードを提案してください。
前もって感謝します!