Tuesday, 15 July 2014

objective c - Randomly choose an item in Obj-C -



objective c - Randomly choose an item in Obj-C -

i have next method changes background color 1 of 3 colors:

- (void) setbackgroundofview { // alter background color uicolor *feijoa = [uicolor colorwithred:0.565 green:0.82 blue:0.478 alpha:1]; /*#90d17a*/ uicolor *turquoise = [uicolor colorwithred:0.2 green:0.78 blue:0.773 alpha:1]; /*#33c7c5*/ uicolor *lavendar = [uicolor colorwithred:0.765 green:0.541 blue:0.898 alpha:1] /*#c38ae5*/ uicolor *randomcolor = random.choose(feijoa, turquoise, lavendar) // in pseudocode }

what right way random.choose(feijoa, turquoise, lavendar)?

you can store colors nsarray , pick 1 of them randomly:

#include <stdlib.h> - (void) setbackgroundofview { // alter background color uicolor *feijoa = [uicolor colorwithred:0.565 green:0.82 blue:0.478 alpha:1]; /*#90d17a*/ uicolor *turquoise = [uicolor colorwithred:0.2 green:0.78 blue:0.773 alpha:1]; /*#33c7c5*/ uicolor *lavendar = [uicolor colorwithred:0.765 green:0.541 blue:0.898 alpha:1] /*#c38ae5*/ nsarray *colors = @[feijoa, turquoise, lavendar]; int index = arc4random_uniform(colors.count); uicolor *randomcolor = colors[index]; }

objective-c

No comments:

Post a Comment