2012-11-03 31 views
0

如何在代碼中更改此數字「63」?隨機CGPoint使用uiimageview

-(void)up 
{ 


    [UIView animateWithDuration:[self randomFloatBetween:1.0 and:0.3] animations:^{ 
     m1.center = CGPointMake(63, 210); 
    }]; 


    [self performSelector:@selector(down) withObject:nil afterDelay:[self randomFloatBetween:1.0 and:0.5]]; 
} 

從我的CGPointMake 63號應該是隨機的。所以對於數字63我需要隨機這些數字:160 & 257.

我的「m1」圖像,出現在CGPointMake(63,210)或257.隨機。 Thnaks很多。

+1

'arc4random()'。 – Kevin

+0

請你解釋一下。我是初學者。謝謝 – user1625435

+0

http://stackoverflow.com/q/160890/900873 – Kevin

回答

1
-(void)up 
{ 
    NSArray *myArray = [NSArray arrayWithObjects:[NSNumber numberWithFloat:63],[NSNumber numberWithFloat:160],[NSNumber numberWithFloat:257], nil]; 
    [UIView animateWithDuration:[self randomFloatBetween:1.0 and:0.3] animations:^{ 
     m1.center = CGPointMake([[myArray objectAtIndex:arc4random() % [myArray count]] floatValue], 210); 
    }]; 
    [self performSelector:@selector(down) withObject:nil afterDelay:[self randomFloatBetween:1.0 and:0.5]]; 
} 

數量也許這會有所幫助。

+0

非常感謝。謝謝。謝謝。謝謝。 :) – user1625435

1

添加所需的號碼一個NSArray,並選擇[yourArray objectAtIndex:(arc4random()%yourArray.count)];

+0

無法正常工作。點63是浮動的。你能寫一個簡單的代碼嗎? – user1625435

+0

我已經爲你解決了這個問題,你應該可以從中編寫代碼。你可以通過[object floatValue]得到一些提示。 – Martol1ni