0

So the BELOW code shows animation of all those colors in the order that I placed it. However, how would I make it that the colors appear in no particular order but instead appear in RANDOM order?

bird.animationImages=[NSArray arrayWithObjects:
                          [UIImage imageNamed:@"red.png"],
                          [UIImage imageNamed:@"brown.png"],
                          [UIImage imageNamed:@"green.png"],
                          [UIImage imageNamed:@"gold.png"],
                          [UIImage imageNamed:@"black.png"], nil];

    [bird setAnimationRepeatCount:0];
    bird.animationDuration=1;
    [bird startAnimating];
Jet
  • 555
  • 1
  • 7
  • 19
  • 1
    See this [SO answer](http://stackoverflow.com/a/10258341/451475) to randomize the array of colors. – zaph Mar 31 '15 at 02:36

3 Answers3

1

- (UIImage *)getRandomImages { NSArray *imagesArray = @[@"image1", @"image2", @"image3"]; NSInteger randomNumber = arc4random_uniform((unsigned int)imagesArray.count - 1); NSString *imageNames = imagesArray[randomNumber]; return [UIImage imageNamed: imageNames]; } The above code will generate the correct random images.

0

If you have created all of these images and added them to the project, then you can just write a helper method to get yourself a random image. It might look like this:

- (UIImage*)getRandomColorImage
{
    NSInteger kNumberOfColors = 5; // or however many color images you have
    NSInteger randomNumber = arc4random() % kNumberOfColors;
    if(randomNumber == 0)
    {
        return [UIImage imageNamed:@"red"];
    }
    else if (randomNumber == 1)
    {
        return [UIImage imageNamed:@"brown"];
    }
    else if (randomNumber == 2)
    {
        return [UIImage imageNamed:@"green"];
    }
    else if (randomNumber == 3)
    {
        return [UIImage imageNamed:@"gold"];
    }
    else
    {
        return [UIImage imageNamed:@"black"];
    }
}

If you wanted to make things even more flexible, you could get rid of the images you added to the project and just create any colored image on the fly with the help of the Core Graphics library and this cool github repo: https://gist.github.com/kylefox/1689973 . The magic there is in these 4 lines (which generate a random color):

CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
Brian Sachetta
  • 3,319
  • 2
  • 34
  • 45
  • Use `arc4random_uniform(kNumberOfColors)` instead of `arc4random() % kNumberOfColors`, this eliminates bias and is cleaner. – zaph Mar 31 '15 at 03:44
0

Simpler getRandomColorImage (just for fun):

- (UIImage *)getRandomColorImage {
    NSArray *colors = @[@"red", @"brown", @"green", @"gold", @"black"];
    NSInteger randomNumber = arc4random_uniform((unsigned int)colors.count);
    NSString *colorName = colors[randomNumber];
    return [UIImage imageNamed: colorName];
}
zaph
  • 111,848
  • 21
  • 189
  • 228