2012-07-17 45 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. 

等等。