问题
I want to generate same Random color for selected cell and background cell. I have successfully implemented it with flowing code . but with this i am getting cell.background color correctly but i want to set light color on each indexpath of selected cell. so, how can i do it ? suppose ,
if (indexpath.row ==1) {
// lightcolor
}
if (indexpath.row == 2) {
// More light color as compare to indexpath.row ==1
}
@interface DisplayViewController () {
DisplayCollectionViewCell *cell;
float randomRed,randomGreen,randomBlue;
}
- (void)viewDidLoad {
randomRed = arc4random() % 255;
randomGreen = arc4random() % 255;
randomBlue = arc4random() % 255;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DisplayCellIphone" forIndexPath:indexPath];
if (indexPath.row == _setRandomIndex) {
// For Selected Color
cell.layer.backgroundColor = [UIColor colorWithRed:randomRed * 0.9 /255.0 green:randomGreen * 0.9 /255.0 blue:randomBlue * 0.9/255.0 alpha:1].CGColor;
} else {
cell.backgroundColor = [UIColor colorWithRed:randomRed/255.0 green:randomGreen/255.0 blue:randomBlue/255.0 alpha:1.0];
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
_count ++;
randomRed = arc4random() % 255;
randomGreen = arc4random() % 255;
randomBlue = arc4random() % 255;
if (indexPath.row == _setRandomIndex) {
if (_count == 0) {
[self addData:4];
}
NSLog(@"Selected Right Cell");
if (_count == 1 || _count == 2) {
_setRandomIndex = arc4random() % 6;
[self addData:6];
// _setRandomIndex = 2;
}
// next cell till 20;
}
回答1:
First keep a UIColor
property (I named it cellColor
) and use the code below
if (indexpath.row ==1) {
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
self.cellColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
if (indexpath.row == 2) {
CGFloat hue, saturation, brightness, alpha;
BOOL success = [self.cellColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
self.cellColor = [self.cellColor initWithHue:hue saturation:saturation/2 brightness:brightness alpha:alpha;
}
Now, in indexPath 1 condition, you create a random color. And in indexPath 2 condition, I got the hue and saturation values and changed the saturation value for random color to make brighter. I hope this could help you.
回答2:
You can use [UIColor colorWithHue: saturation: brightness: alpha:]
and change the brightness and saturation values. Set random values for the parameters, and using the answer in this post get components of your UIColor and generate colors by changing only the brightness and saturation values keeping hue value constant
Is there function to convert UIColor to Hue Saturation Brightness?
来源:https://stackoverflow.com/questions/32480260/how-to-set-dark-and-light-random-color-in-uicollectionviewcell