gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:29:24 26 4
gpt4 key购买 nike

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

I want to achieve is first image & what I was able to achieve is second

enter image description here enter image description here

Code I wrote for what I was able to achieve is

  BackgroundLayer.h

+(CAGradientLayer*) CustomHorBlack;

   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;

}

TableCell.m

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

Code I wrote for what I want to achieve is but doesn't find what I want is

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


}


}

最佳答案

我可以用下面的代码实现

TableViewCell.h

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

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

@end

TableViewCell.m

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

ViewController.m

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

Refer to screenshot

enter image description here

关于ios - 如何在 iOS 中使用 CAGradientLayer 在 UITableViewCell 中设置多个渐变颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30478975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com