2012-02-25 77 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