0

我有一个 NSMutableArray,我用这样的 UIImageViews 填充:

NSMutableArray *digitArray;

spaces = [[NSMutableArray alloc] initWithCapacity:16];

[spaces addObject:space1];
[spaces addObject:space2];
[spaces addObject:space3];
[spaces addObject:space4];
[spaces addObject:space5];
[spaces addObject:space6];
[spaces addObject:space7];
[spaces addObject:space8];
[spaces addObject:space9];
[spaces addObject:space10];
[spaces addObject:space11];
[spaces addObject:space12];
[spaces addObject:space13];
[spaces addObject:space14];
[spaces addObject:space15];
[spaces addObject:space16];  

然后我为每个 UIImageViews 分配一个图像,如下所示:

[(UIImageView *)spaces[0] setImage:one];
[(UIImageView *)spaces[1] setImage:two];
[(UIImageView *)spaces[2] setImage:three];
[(UIImageView *)spaces[3] setImage:four];
[(UIImageView *)spaces[4] setImage:five];
[(UIImageView *)spaces[5] setImage:six];
[(UIImageView *)spaces[6] setImage:seven];
[(UIImageView *)spaces[7] setImage:eight];
[(UIImageView *)spaces[8] setImage:nine];
[(UIImageView *)spaces[9] setImage:ten];
[(UIImageView *)spaces[10] setImage:eleven];
[(UIImageView *)spaces[11] setImage:twelve];
[(UIImageView *)spaces[12] setImage:thirteen];
[(UIImageView *)spaces[13] setImage:fourteen];
[(UIImageView *)spaces[14] setImage:fifteen];
[(UIImageView *)spaces[15] setImage:sixteen];

因此,将此作为我的默认组合,然后我希望能够随机更改分配给哪个 UIImageVIew 的图像。任何人都可以建议这样做的方法吗?

提前致谢!

乔治

4

3 回答 3

0

认为这应该这样做

NSMutableArray *temp = [[NSMutableArray alloc] init];
while(space.count > 0){

    int index = arc4random_uniform(space.count);
    [temp addObject:[spaces objectAt:index]];
    [space removeObjectAtIndex:index];
}

spaces = temp;

havnt 测试了代码,但它从空间中获取 1 个随机 ImageView 并将其放入一个临时数组中,然后一旦将空间中的所有对象放入 temp 中,它就会将其分配回空间

于 2013-03-12T14:22:36.213 回答
0

numbers对于任何感兴趣的人,我通过创建第二个数组并用图像填充它来实现我想要的。然后我随机化了第二个数组:

 for (int i = 0; i < 16; i++) {
    int j = arc4random() % 16;
    [numbers exchangeObjectAtIndex:i withObjectAtIndex:j];
}

然后我将第一个数组中各种 UIImageViews 的图像分配为第二个数组的图像

     [(UIImageView *)spaces[0] setImage:numbers[0]];
     [(UIImageView *)spaces[1] setImage:numbers[1]];
     .... etc

由于数字数组的混淆,等的内容numbers[0]每次numbers[1]都会有所不同。

不过还是谢谢大家的回答!

于 2013-03-12T15:54:27.283 回答
0
UIImageView *firstImage = [spaces objectAtIndex:arc4random_uniform(16)];

UIImageView *secondImage = [spaces objectAtIndex:arc4random_uniform(16)];

UIImageView *tempImage = firstImage;

firstImage.image = secondImage.image;

secondImage.image = tempImage.image;

不过可能有更短的方法。但这应该可以解决问题。随机意味着,两个图像可能相同。这并没有什么坏处,但实际上什么都不会改变。

于 2013-03-12T14:57:31.670 回答