-1

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];
}
StuartM
  • 6,743
  • 18
  • 84
  • 160
  • Create the `NSMutableArray`, remove the color you used. – Larme May 07 '14 at 14:08
  • Who voted down, its a perfectly valid question – StuartM May 07 '14 at 14:19
  • @StuartM: Validate, yes, in its form, information given, clear question, etc. But seems like a duplicate. That may explain some down votes. – Larme May 07 '14 at 14:29
  • I think that you are voted down because your question is very similar to topic that commented above. – Vlad Papko May 07 '14 at 14:29
  • but this is very different, this is a View not a View Controller so I cannot create the starting array in viewDidLoad. Also they should vote to close not vote down. – StuartM May 07 '14 at 14:35
  • @StuartM I've posted possible solution for your issue. Please check it. – Vlad Papko May 07 '14 at 14:44
  • 1
    The duplicate explains how to get a unique random item out of an array. If you are unsure of where to initialize the array, that's really a separate issue and should be researched separately and posted as its own question should your research arrive at a dead end. – Carl Veazey May 08 '14 at 03:48

2 Answers2

2

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;
}
Vlad Papko
  • 13,184
  • 4
  • 41
  • 57
1

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

[mutableArray removeObject:color];
Nazariy Vlizlo
  • 778
  • 7
  • 16
  • Exactly what I was going to suggest. (voted) – Duncan C May 07 '14 at 14:14
  • At what point would you suggest to remove the object? – StuartM May 07 '14 at 14:21
  • from the documentation: Removes all occurrences in the array of a given object. – Nazariy Vlizlo May 07 '14 at 14:24
  • Is it as simple as after I have used it. I have checked the example link in the comments but it is different this is a View class not a view controller so I do not create the objects in viewDidLoad. In terms of my example code in the question, where would I create a mutableCopy and remove the object please?? – StuartM May 07 '14 at 14:40
  • I cannot remove in the method before it is returned... – StuartM May 07 '14 at 14:41
  • create mutableArray like property in your Controller and make some method with removeObject – Nazariy Vlizlo May 07 '14 at 14:43
  • It is not controlled by the controller this is a storyboard- uiview class, it has been answered correctly above. Thanks – StuartM May 07 '14 at 14:50