0

I've created a UIImage that changes image every 8 seconds, however right now its made so that the images are hard coded in a specific order, my question is how can I untiles the arc 4 random tool or another way to make it so that the order is completely random, here is my code as is:

My .h:

#import <UIKit/UIKit.h>


@interface HomeViewController : UIViewController {
NSTimer *myTimer;
IBOutlet UIImageView *imageview;
}
@end

And in my .m:

- (void)viewDidLoad
{
[super viewDidLoad];

     myTimer = [NSTimer scheduledTimerWithTimeInterval:8.00 target:self     selector:@selector(changeImage) userInfo:nil repeats:YES];


_images = @[@"Background 2.png", @"Background 3.png", @"Background 4.png", @"Background   5.png", @"Background 6.png", @"Background 7.png", @"Background 9.png", @"RSGC Crest.png"];

}

- (void)changeImage
{
static int counter = 0;
if([_images count] == counter+1)
{
    counter = 0;
}
imageview.image = [UIImage imageNamed:[_images objectAtIndex:counter]];

                   counter++;
}

Any help would be hugely appreciated.

4

1 回答 1

0

你会arc4random_uniform()用来选择这样的随机图像

int imageNumber = arc4random_uniform(8); //because you have 8 image names in the array
NSString *imageName = _images[imageNumber];
imageView.image = [UIImage imageNamed:imageName];

感谢@danielbeard 使用arc4random_uniform()而不是arc4random().

于 2013-07-20T17:35:58.297 回答