如何在iOS中使用CAGradientLayer在UITableViewCell中设置多重渐变颜色?

如何在iOS中使用CAGradientLayer在UITableViewCell中设置多重渐变颜色?,ios,objective-c,uitableview,cagradientlayer,Ios,Objective C,Uitableview,Cagradientlayer,我想设置多个渐变颜色,每行都有唯一的渐变颜色,如下图所示: 我想实现的是第一个形象&我能够实现的是第二个形象 我为我能够实现的目标编写的代码是 +(CAGradientLayer*)黑色 表细胞 我为我想要实现的目标而编写的代码,但没有找到我想要的 -(void)从NIB唤醒{ if(!checkVariable){ 变量=0; checkVariable=TRUE; } [[self-contentView]superview]setClipsToBounds:是]; CAGradientL

我想设置多个渐变颜色,每行都有唯一的渐变颜色,如下图所示:

我想实现的是第一个形象&我能够实现的是第二个形象

我为我能够实现的目标编写的代码是

+(CAGradientLayer*)黑色

表细胞

我为我想要实现的目标而编写的代码,但没有找到我想要的

-(void)从NIB唤醒{
if(!checkVariable){
变量=0;
checkVariable=TRUE;
}
[[self-contentView]superview]setClipsToBounds:是];
CAGradientLayer*bgLayer;
//=[背景层黑色];
用于(变量=0;变量<10;变量++){
开关(可变)
{
案例0:
bgLayer=[背景层类别健康丁基];
//变量=变量+1;
打破
案例1:
bgLayer=[背景层分类];
//变量=变量+1;
打破
案例2:
bgLayer=[背景层类别计算机];
//变量=变量+1;
打破
案例3:
bgLayer=[背景层类别教育];
//变量=变量+1;
打破
案例4:
bgLayer=[BackgroundLayer CategoryElectronics];
//变量=变量+1;
打破
案例5:
bgLayer=[背景层类别娱乐];
变量=变量+1;
打破
案例6:
bgLayer=[BackgroundLayer CategoryGroceries];
//变量=变量+1;
打破
案例7:
bgLayer=[背景层类别健康丁基];
//变量=变量+1;
打破
案例8:
bgLayer=[BackgroundLayer CategoryHome];
//变量=变量+1;
打破
案例9:
bgLayer=[BackgroundLayer CategoryResturant];
//变量=变量+1;
打破
案例10:
bgLayer=[背景层类别];
//变量=0;
打破
违约:
bgLayer=[背景层类别];
//变量=变量+1;
打破
}
//bgLayer.frame=self.contentView.bounds;
bgLayer.frame=CGRectMake(-7,0,[Util window_width],72);
if([Util isiPhone6]){
bgLayer.frame=CGRectMake(-7,0,[Util window_width],84);
}
else if([Util isiPhone6PlusDevice]){
bgLayer.frame=CGRectMake(-7,0,[Util window_width],93);
}
[self.CatImageView.layer insertSublayer:bgLayer atIndex:variable];
变量=变量+1;
}
}

我可以用下面的代码实现这一点

TableViewCell.h

TableViewCell.m

ViewController.m

请参阅屏幕截图


您当前的代码到底出了什么问题?他可能在分享他的知识。。隐马尔可夫模型。。但这是一个好主意……)只能实现一种渐变颜色,而不是多重渐变颜色,但我想使用上图所示的多重渐变颜色,这是您执行
[BackgroundLayer CategoryHealthBut]的问题
缺少代码?没有缺少的代码我希望每个单元格中都有多个渐变渐变,如上图所示。每个单元格中使用的颜色是特定的吗?不,我只需要使用多个渐变。你认为每个单元格都有不同的渐变很难吗?我不认为这很难,但我无法做到有问题的提到。你能做到吗?
  BackgroundLayer.h
   BackgroundLayer.m
+ (CAGradientLayer*) CustomHorBlack {
    UIColor *colorOne = [UIColor colorWithRed:(0.0) green:(0.0 ) blue:(0.0) alpha:0.1];
    UIColor *colorTwo = [UIColor colorWithRed:(0.0) green:(0.0 ) blue:(0.0) alpha:0.2];
    UIColor *colorThree = [UIColor colorWithRed:(0.0) green:(0.0 ) blue:(0.0)  alpha:0.3];
    UIColor *colorFour = [UIColor colorWithRed:(0.0) green:(0.0 ) blue:(0.0) alpha:0.4];
    UIColor *colorFive = [UIColor colorWithRed:(0.0) green:(0.0 ) blue:(0.0) alpha:0.5];
    UIColor *colorSix = [UIColor colorWithRed:(0.0) green:(0.0 ) blue:(0.0)  alpha:0.6];

    NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor,colorThree.CGColor,colorFour.CGColor, colorFive.CGColor,colorSix.CGColor, nil];

    CAGradientLayer *headerLayer = [CAGradientLayer layer];
    headerLayer.colors = colors;
    [headerLayer setStartPoint:CGPointMake(0.0, 0.5)];
    [headerLayer setEndPoint:CGPointMake(1.0, 0.5)];



    return headerLayer;

}
-(void)awakeFromNib{
    [[[self contentView] superview] setClipsToBounds:YES];
    CAGradientLayer *bgLayer = [BackgroundLayer CustomHorBlack];
 //   bgLayer.frame = self.contentView.bounds;
    bgLayer.frame = CGRectMake(-7, 0, [Util window_width], 72);

    if ([Util isiPhone6]) {
        bgLayer.frame = CGRectMake(-7, 0, [Util window_width], 84);
    }
    else if ([Util isiPhone6PlusDevice]){
        bgLayer.frame = CGRectMake(-7, 0, [Util window_width], 93);
    }

    [self.CatImageView.layer insertSublayer:bgLayer atIndex:0];
}
-(void)awakeFromNib{

    if (!checkVariable) {
        variable=0;
        checkVariable=TRUE;
    }

    [[[self contentView] superview] setClipsToBounds:YES];
    CAGradientLayer *bgLayer;
    //= [BackgroundLayer CustomHorBlack];

    for(variable = 0; variable < 10; variable++) {

        switch(variable)
        {
            case 0:
                bgLayer = [BackgroundLayer CategoryHealthButy];
                //variable = variable + 1;


                break;
            case 1:
               bgLayer = [BackgroundLayer CategoryClothing];
               // variable = variable + 1;

                break;
            case 2:
               bgLayer = [BackgroundLayer CategoryComputer];
               // variable = variable + 1;

                break;

            case 3:
                bgLayer = [BackgroundLayer Categoryeducation];
                //variable = variable + 1;


                break;

            case 4:
                bgLayer = [BackgroundLayer CategoryElectronics];
               // variable = variable + 1;


                break;
            case 5:
                bgLayer = [BackgroundLayer CategoryEntertainment];
                variable = variable + 1;

                break;
            case 6:
                bgLayer = [BackgroundLayer CategoryGroceries];
              //  variable = variable + 1;

                break;

            case 7:
                bgLayer = [BackgroundLayer CategoryHealthButy];
              //  variable = variable + 1;


                break;

            case 8:
                bgLayer = [BackgroundLayer CategoryHome];
             //   variable = variable + 1;

                break;
            case 9:
                bgLayer = [BackgroundLayer CategoryResturant];
               // variable = variable + 1;

                break;

            case 10:
                bgLayer = [BackgroundLayer CategoryToys];
               // variable = 0;


                break;

            default:
                bgLayer = [BackgroundLayer CategoryToys];
               // variable = variable + 1;


                break;
        }



 //   bgLayer.frame = self.contentView.bounds;
    bgLayer.frame = CGRectMake(-7, 0, [Util window_width], 72);

    if ([Util isiPhone6]) {
        bgLayer.frame = CGRectMake(-7, 0, [Util window_width], 84);
    }
    else if ([Util isiPhone6PlusDevice]){
        bgLayer.frame = CGRectMake(-7, 0, [Util window_width], 93);
    }

    [self.CatImageView.layer insertSublayer:bgLayer atIndex:variable];

        variable = variable + 1;


    }


}
@interface TableViewCell : UITableViewCell
{
    UIImageView *imageView;
    UIView *viewGradient;
    CAGradientLayer *gradientLayer;
}

- (void)setImage:(UIImage *)image andColor:(UIColor *)color;

@end
- (void)awakeFromNib {
    [self.contentView setFrame:CGRectMake(0, 0, 320, 44)];
        imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
    [imageView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth ];
            [self.contentView addSubview:imageView];

            viewGradient = [[UIView alloc] initWithFrame:self.contentView.bounds];
        [viewGradient setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth ];
            [self.contentView addSubview:viewGradient];
            gradientLayer = [CAGradientLayer layer];
            gradientLayer.frame = viewGradient.bounds;
            [gradientLayer setStartPoint:CGPointMake(0.0, 0.5)];
            [gradientLayer setEndPoint:CGPointMake(1.0, 0.5)];
            [viewGradient.layer insertSublayer:gradientLayer atIndex:0];
}
 - (void)setImage:(UIImage *)image andColor:(UIColor *)color
    {
        [imageView setImage:image];
        gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[color CGColor], nil];
    }
         - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        switch (indexPath.row)
        {
            case 0:
                [cell setImage:[UIImage imageNamed:@"Grass"] andColor:[UIColor whiteColor]];
                break;
            case 1:
                [cell setImage:[UIImage imageNamed:@"House"] andColor:[UIColor greenColor]];
                break;
            case 2:
                [cell setImage:[UIImage imageNamed:@"Sky"] andColor:[UIColor redColor]];
                break;

            default:
                break;
        }
        return cell;
    }