随机选择三个图像

时间:2013-05-10 19:34:05

标签: objective-c cocoa-touch random

我想随机选择三张图片之一,我相信我应该使用arc4random(),但我无法确定。这是我的简单代码:

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

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:1)

试试这个:

NSArray *imageArray = [[NSArray alloc] initWithObjects:image,image1,image2, nil];
SPImage *randomImage = [imgArray objectAtIndex:arc4random_uniform(3)];