我如何创建一个随机图像生成器,它不会重复图像,直到它们通过它们一次?
例如:
- (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”?
感谢您的帮助!
答案 0 :(得分:1)
将图像名称存储在NSMutableArray中并随机播放阵列,例如通过类似的方法 this SO answer
然后遍历数组。
答案 1 :(得分: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.
等等。