gpt4 book ai didi

iphone - 添加 CAGradientLayer 子层时,UITableViewCell 不会拾取 UITableViewCellSelectionStyleBlue

转载 作者:行者123 更新时间:2023-11-29 11:21:42 28 4
gpt4 key购买 nike

我有一个 UITableViewCell,我已经使用 CAGradientLayer 添加了渐变。这工作正常,但表格单元格在被选中时不会变成蓝色,即使在将它的 selectionStyle 设置为 UITableViewCellSelectionStyleBlue 之后也是如此。如果我不添加渐变层,它工作正常。

有没有办法让这些项目协同工作?

这是我在 cellForRowAtIndexPath 中的代码:

    //cell gradient        
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(10, 0, 300, 50);
gradient.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1] CGColor],
(id)[[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1] CGColor],
(id)[[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1] CGColor],
(id)[[UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1] CGColor],
nil];
gradient.locations = [NSArray arrayWithObjects:
(id)[NSNumber numberWithFloat:0.00],
(id)[NSNumber numberWithFloat:0.50],
(id)[NSNumber numberWithFloat:0.51],
(id)[NSNumber numberWithFloat:1.0],
nil];
[cell.layer insertSublayer:gradient atIndex:0];


//bring back rounded corners by creating a masklayer
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:gradient.bounds byRoundingCorners:UIRectCornerBottomRight|UIRectCornerBottomLeft cornerRadii:CGSizeMake(8, 8)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = gradient.bounds;
maskLayer.path = maskPath.CGPath;
gradient.mask = maskLayer;

//add the cell shadow
cell.layer.shadowColor = [[UIColor blackColor] CGColor];
cell.layer.shadowRadius = 3;
cell.layer.shadowOpacity = 0.5;
cell.layer.shadowOffset = CGSizeMake(0, 0);

cell.selectionStyle = UITableViewCellSelectionStyleBlue;

最佳答案

看起来你可能将渐变放在 View 上,当它被选中时会发生变化 - UITableViewCellSelectionStyleBlue View 可能会出现,但你就是看不到它(测试它的一种方法是只有渐变覆盖部分您的单元格-如果其他部分在被选中时改变了颜色,那么这就是问题所在)。

如果这是问题所在,那么您可以在通过子类化 UITableViewCell 选择单元格时为单元格绘制您自己的自定义绘图,或者您可以在单元格被选中时让渐变消失(并在取消选择单元格时重新出现)。

关于iphone - 添加 CAGradientLayer 子层时,UITableViewCell 不会拾取 UITableViewCellSelectionStyleBlue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6783623/

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