2012-02-25 78 views
0

我正在構建一個應用程序,其中有8個UI按鈕,使用arc4random給出所有隨機座標。一直試圖弄清楚如何阻止它們重疊。arc4random - 8個按鈕重疊

任何想法或幫助將非常感激,因爲它是阻止我的應用程序的唯一東西!

感謝

回答

0

你需要的可能位置的陣列不重疊:

CGPoint possibleLocations[] = { 
    { 0, 0 }, 
    { 25, 25 }, 
    { 25, 50 }, 
    // etc. 
} 

int numLocations = sizeof(possibleLocations)/sizeof(CGPoint); 
BOOL takenLocations[numLocations]; 

CGPoint finalLocation; 
int index; 

while ((takenLocations[index = arc4random_uniform(numLocations])) 
{ 
} 

takenLocations[index] = YES; 
finalLocation = possibleLocations[index]; 
+0

是啊!當然。並感謝這麼快速的回覆。一直試圖將這個實現到我的代碼中,但收效甚微。在上面的提取中,我是否包含按鈕? – SirJoeyMichaels 2012-02-25 20:11:46

+0

任何人都可以請幫忙嗎?我真的很掙扎! – SirJoeyMichaels 2012-03-02 16:38:44