-1

我想从三个图像中随机选择一个,我相信我应该使用arc4random(),但我不能确定。这是我的简单代码:

SPImage *image = [SPImage imageWithTexture:[balloonTextures objectAtIndex:0]];
SPImage *image1 = [SPImage imageWithTexture:[balloonTextures objectAtIndex:0]];
SPImage *image2 = [SPImage imageWithTexture:[balloonTextures objectAtIndex:0]];
4

2 回答 2

2

问题解决了 !!

这将完全符合您的要求,

SPImage *image = [SPImage imageWithTexture:[balloonTextures objectAtIndex:0]];
SPImage *image1 = [SPImage imageWithTexture:[balloonTextures objectAtIndex:0]];
SPImage *image2 = [SPImage imageWithTexture:[balloonTextures objectAtIndex:0]];

NSArray *imageArray = [[NSArray alloc] initWithObjects:image,image1,image2, nil];
int random = arc4random() % 3;

SPImage *myRandomImage = [imageArray objectAtIndex:random];
于 2013-05-10T19:36:36.850 回答
1

尝试这个:

NSArray *imageArray = [[NSArray alloc] initWithObjects:image,image1,image2, nil];
SPImage *randomImage = [imgArray objectAtIndex:arc4random_uniform(3)];
于 2013-05-10T19:46:43.660 回答