2010-12-03 65 views
0

我想用隨機座標在屏幕上移動一個球。但我的對象繞x軸移動!這裏是我的代碼:隨機移動一個球

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.70]; 
[UIView setAnimationRepeatCount:1000]; 
[UIView setAnimationRepeatAutoreverses:YES]; 

CGFloat x = (CGFloat) random()/(CGFloat) RAND_MAX; 
CGFloat y = (CGFloat) random()/(CGFloat) RAND_MAX; 

CGPoint squarePostion = CGPointMake(y, x); 
blue3.center = squarePostion; 

[UIView commitAnimations]; 

編輯:

我我的代碼更改爲這可是不行的〜:

CGFloat x = (CGFloat) random()/(CGFloat) RAND_MAX * self.view.bounds.size.width; 
CGFloat y = (CGFloat) random()/(CGFloat) RAND_MAX * self.view.bounds.size.height; 

CGPoint squarePostion = CGPointMake(y, x); 

同樣的情況!

回答

1

您將所有值縮放到0 ... 1,所以我希望您的球實際移動到(0,0)並留在那裏。另外,你可以讓你的x座標換成CGPointMake。你需要擴展你的價值,你希望它是在該地區的隨機位置在你的視圖(如果blue3containerView子視圖 - 這可能是自這裏):

CGFloat x = (CGFloat) random()/(CGFloat) RAND_MAX * containerView.bounds.size.width; 
CGFloat y = (CGFloat) random()/(CGFloat) RAND_MAX * containerView.bounds.size.height; 

CGPoint squarePostion = CGPointMake(x, y); 
+0

請看到我的編輯問題 – 2010-12-03 09:28:38