问题
Okay well I have made a couple threads asking how to display images randomly/pick a random image. What I have came to realize is that I do not know how to combine these two methods together.
I have 5 images in an array.
My .h file:
@property (strong, nonatomic) NSArray *imageArray;
My .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImage *image1 = [UIImage imageNamed:@"image-1.png"];
UIImage *image2 = [UIImage imageNamed:@"image-2.png"];
UIImage *image3 = [UIImage imageNamed:@"image-3.png"];
UIImage *image4 = [UIImage imageNamed:@"image-4.png"];
UIImage *image5 = [UIImage imageNamed:@"image-5.png"];
_imageArray = @[image1, image2, image3, image4, image5];
}
This is what I have right now, I have been playing around with other code ive found elsewhere but have had no success so left it out.
Now that you see what I have, this is what im trying to do: I need to use a method that will pick one of my 5 images randomly from my array and then display it in a random position in my view.
I also need to repeat this loop, but with constraints. I need each image to have a "value" such as: image-1 equal to 1, image-2 equal to 2, image-3 equal to 3, image-4 equal to 4, and image-5 equal to 5. And have the loop repeat until the images displayed equals a total value of 50.
Im not really sure where to begin on what methods to use. Im sure picking and displaying randomly is easy to some of you but the values and repeating until equaling 50 seems complex. So any and all help is greatly appreciated! Thanks in advance to anyone who can help, im new to coding so if you can explain why you used your code it would help even more! Thanks!
Edit: This guy tried helping me in my other thread adding the whole picking a random image as well. His response is here: How to display multiple UIImageViews and I used his code but nothing happened. I am not sure if his code is wrong somewhere or if I did something wrong.
回答1:
Use a recursive loop and an int to keep track of your progress towards your desired count of 50.
Each time through the loop, you'll want to:
- Generate a new random number (0-4)
- Use your random number to select an image from your array
- Add your random number to your int and check to see if you've hit 50.
- If you've hit 50, you're done
- If you haven't hit 50, do it again.
Something like this:
//in your .h declare an int to track your progress
int myImgCount;
//in your .m
-(void)randomizeImages {
//get random number
int randomImgNum = arc4random_uniform(5);
//use your random number to get an image from your array
UIImage *tempImg = [_imageArray objeactAtIndex:randomImgNum];
//add your UIImage to a UIImageView and place it on screen somewhere
UIImageView *tempImgView = [[UIImageView alloc] initWithImage:tempImg];
//define the center points you want to use
tempImgView.center = CGPointMake(yourDesiredX,yourDesiredY);
[self addSubview:tempImgView];
[tempImgView release];
//increment your count
myImgCount = myImgCount+(randomImgNum+1);
//check your count
if (myImgCount<50) {
[self randomizeImages]; //do it again if not yet at 50
}
}
Something like that should work for you.
回答2:
this code uses the M42RandomIndexPermutation class available on github:
https://github.com/Daij-Djan/DDUtils/tree/master/model/M42RandomIndexPermutation%20%20%5Bios%2Bosx%5D
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray *images = @[@"image-1", @"image-2", @"image-3", @"image-4", @"image-5"];
M42RandomIndexPermutation *permutation = [[M42RandomIndexPermutation alloc] initWithCount:images.count usingSeed:[NSDate date].timeIntervalSince1970];
for(int i = 0; i<images.count; i++) {
NSInteger index = [permutation next];
NSLog(@"%@",images[index]);
}
//TODO assign them to imageviews instead of logging them
}
return 0;
}
回答3:
Maybe this can help:
int index = arc4random % ([_imageArray count] - 1);
imageView.image = [_imageArray objectAtIndex:index];
Cheers!
来源:https://stackoverflow.com/questions/19530918/how-to-pick-a-random-image-from-nsarray-and-display-randomly-on-view