2012-07-17 46 views
0

我将如何创建一个随机图像发生器,不重复的图像,直到它已经通过他们所有的一次?随机图像发生器,不重复xcode

例如:

- (IBAction为)randomimagebutton {

int randomimages = rand() % 5; 

switch (randomimages) { 

    case 0: 
     imageview.image = [UIImage imageNamed:@"IMAGE_001.png"]; 
     break; 
    case 1: 
     imageview.image = [UIImage imageNamed:@"IMAGE_002.png"]; 
     break; 
    case 2: 
     imageview.image = [UIImage imageNamed:@"IMAGE_003.png"]; 
     break; 
    case 3: 
     imageview.image = [UIImage imageNamed:@"IMAGE_004.png"]; 
     break; 
    case 4: 
     imageview.image = [UIImage imageNamed:@"IMAGE_005.png"]; 
     break; 

default: 

     break; 

} 

如果图像中出现的顺序为:1,4,2,5,4,3我怎么能确保在直到显示所有5张图像后,“4”才会再次出现?

感谢您的帮助!

回答

1

将图像名称存储在NSMutableArray中,然后对数组进行混洗。如 this SO answer

然后遍历数组。

0

创建的NSMutableArray和每当选择一个随机数上它把0,1,2,3,4然后将其取下像这样:

[myNSMutableArray removeObjectAtIndex:myRandNumber]; 


To fill the gap, all elements beyond index are moved by subtracting 1 from their index. 

然后,只需通过执行选择新的随机:

int randomimages = rand() % 4; //4 would of course be a int var that you substract everytime a new random is generated. 

等等。