-1
我正在創建一個遊戲,其中的骰子動作顯示在一個圓圈中。當用戶單擊輪子時,它會旋轉並停在隨機位置。 我搜索,發現一些代碼,我試圖實現,如: -像輪子動畫一樣旋轉圖像並在隨機位置停止
[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];
}
但車輪停在同一位置,每次也並不總是動畫。請爲我提供一些方法或示例代碼來實現所需的功能。
在此先感謝!
我有點困惑;是'rotateDeviceMethod:'中包含的頂級代碼(動畫代碼)? – pasawaya
它是方法名稱。 – Gypsa