0

我将如何创建一个随机图像生成器,它不会重复图像,直到它通过它们一次?

例如:

-(IBAction)随机图像按钮 {

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”?

谢谢你的帮助!

4

2 回答 2

1

将图像名称存储在 NSMutableArray 中并打乱数组,例如通过类似 这样的方法 SO answer

然后循环遍历数组。

于 2012-07-17T13:57:53.107 回答
0

Create a NSMutableArray and put 0, 1, 2, 3, 4 on it then whenever a random number is selected remove it like this:

[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.

等等。

于 2012-07-17T13:58:55.047 回答