gpt4 book ai didi

ios - 如何在 ios 中重新加载时在动态表格单元格中应用渐变颜色层

转载 作者:可可西里 更新时间:2023-11-01 00:59:15 25 4
gpt4 key购买 nike

我想创建渐变色代码并设置到高度根据数据变化的动态表格 View 单元格中。当我要添加到表格单元格的图层时,我使用了图层。表格数据加载后,我需要准确的表格 View 单元格高度。

-(void)setTableData:(NSMutableArray *)tableData{

tableHeightConstraints.constant = cellHeight * tableData.count;

_tableData = tableData;

[self.tableView reloadData];

[self layoutIfNeeded];

CAGradientLayer *bgLayer = [self greyGradient];
bgLayer.cornerRadius = 5.0f;
bgLayer.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
self.backgroundImageViewTc.image = [self greyImageFromGradient:bgLayer];

[self layoutIfNeeded];
}


+ (CAGradientLayer*) greyGradient {

UIColor *colorOne = [UIColor colorWithWhite:0.9 alpha:1.0];
UIColor *colorTwo = [UIColor colorWithHue:0.625 saturation:0.0 brightness:0.85 alpha:1.0];
UIColor *colorThree = [UIColor colorWithHue:0.625 saturation:0.0 brightness:0.7 alpha:1.0];
UIColor *colorFour = [UIColor colorWithHue:0.625 saturation:0.0 brightness:0.4 alpha:1.0];

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

NSNumber *stopOne = [NSNumber numberWithFloat:0.0];
NSNumber *stopTwo = [NSNumber numberWithFloat:0.02];
NSNumber *stopThree = [NSNumber numberWithFloat:0.99];
NSNumber *stopFour = [NSNumber numberWithFloat:1.0];

NSArray *locations = [NSArray arrayWithObjects: stopOne, stopTwo, stopThree, stopFour, nil];
CAGradientLayer *headerLayer = [CAGradientLayer layer];
headerLayer.colors = colors;
headerLayer.locations = locations;

return headerLayer;

}

+ (UIImage *) greyImageFromGradient:(CAGradientLayer*) layer{

UIGraphicsBeginImageContext(layer.bounds.size);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;
}

此代码无法创建精确尺寸的图像,因此变得模糊。

最佳答案

Here u can find ur solution let me know if any issue

- (void)tableView:(UITableView )tableView willDisplayCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath *)indexPath{

TVCell mycell = (TVCell )cell;

CGSize size = CGSizeMake(mycell.cellBkView.frame.size.width, mycell.cellBkView.frame.size.height);

CAGradientLayer *bgLayer = [BackgroundLayer greyGradient];
bgLayer.cornerRadius = 5.0f;
bgLayer.frame = CGRectMake(0, 0, size.width, size.height);
mycell.cellBkView.backgroundColor = [UIColor colorWithPatternImage:[BackgroundLayer greyImageFromGradient:bgLayer]];
}

关于ios - 如何在 ios 中重新加载时在动态表格单元格中应用渐变颜色层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37920590/

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