如何在 UICollectionViewCell 中设置深色和浅色随机颜色? [英] How to set dark and light random color in UICollectionViewCell?

查看:19
本文介绍了如何在 UICollectionViewCell 中设置深色和浅色随机颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为选定的单元格和背景单元格生成相同的随机颜色.我已经用流畅的代码成功地实现了它.但是有了这个,我正确地获得了 cell.background 颜色,但我想在所选单元格的每个索引路径上设置浅色.那么,我该怎么做呢?假设,

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;
   }

推荐答案

您可以使用 [UIColor colorWithHue:饱和度:亮度: alpha:]

并更改亮度和饱和度值.为参数设置随机值,并使用这篇文章中的答案获取 UIColor 的组件并通过仅更改亮度和饱和度值保持色调值不变来生成颜色

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

是否有将 UIColor 转换为色相饱和度的函数亮度?

这篇关于如何在 UICollectionViewCell 中设置深色和浅色随机颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆