Question

I have a custom view, which is a circle view. The view has a custom UIColor randomColor background from another method.

Issue - What I am trying to do is only have the color used once. In this case I have three circle views and 6 colors. So I am looking for one color (randomly selected) for each of the three circle views. What is the best way to accomplish?

-(void)setupView {
    self.translatesAutoresizingMaskIntoConstraints = NO;
    float newRadius = self.frame.size.width/2;
    self.layer.cornerRadius = newRadius; 
    self.layer.masksToBounds= YES;

    self.layer.borderWidth = 5;
    self.layer.borderColor = [UIColor colorWithRed:0.138 green:0.225 blue:1.000 alpha:1.000].CGColor;


    self.backgroundColor = [self randomColor];

    [self setupLabel];
}


-(UIColor *)randomColor {
    static NSArray *__colors = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __colors = @[
                     [UIColor colorWithRed:.11372549  green:.819607843 blue:.819607843 alpha:1.0], //29,209,99
                     [UIColor colorWithRed:.882352941 green:.466666667 blue:.709803922 alpha:1.0], //225,119,181
                     [UIColor colorWithRed:.647058824 green:.164705882 blue:.482352941 alpha:1.0], //165,42,123
                     [UIColor colorWithRed:.482352941 green:.17254902  blue:.733333333 alpha:1.0], //123,44,187
                     [UIColor colorWithRed:.219607843 green:.098039216 blue:.698039216 alpha:1.0], //56,25,178
                     [UIColor colorWithRed:.678431373 green:.843137255 blue:.274509804 alpha:1.0]  //173,215,70
                     ];
    });
    int index = arc4random_uniform((uint32_t)__colors.count);
    return __colors[index];
}
Was it helpful?

Solution

Okey, If you want to avoid duplication you need remove color from array if it selected.
Try this:

-(UIColor *)randomColor {
    static NSMutableArray *__colors = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __colors = [NSMutableArray arrayWithArray:@[[UIColor colorWithRed:.11372549  green:.819607843 blue:.819607843 alpha:1.0], //29,209,99
                                                    [UIColor colorWithRed:.882352941 green:.466666667 blue:.709803922 alpha:1.0], //225,119,181
                                                    [UIColor colorWithRed:.647058824 green:.164705882 blue:.482352941 alpha:1.0], //165,42,123
                                                    [UIColor colorWithRed:.482352941 green:.17254902  blue:.733333333 alpha:1.0], //123,44,187
                                                    [UIColor colorWithRed:.219607843 green:.098039216 blue:.698039216 alpha:1.0], //56,25,178
                                                    [UIColor colorWithRed:.678431373 green:.843137255 blue:.274509804 alpha:1.0]  //173,215,70
                                                    ]];
    });
    int index = arc4random_uniform((uint32_t)__colors.count);

    UIColor *color = __colors[index];
    [__colors removeObjectAtIndex:index];
    return color;
}

OTHER TIPS

try to hold colours in NSMutableArray, and if you use this colour then delete it from array:

[mutableArray removeObject:color];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top