我有一个 UITableViewCell 子类,我添加了这样的叠加渐变:
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// add a layer that overlays the cell adding a subtle gradient effect
_gradientLayer = [CAGradientLayer layer];
_gradientLayer.frame = self.bounds;
_gradientLayer.colors = @[(id)[[UIColor colorWithWhite:1.0f alpha:0.2f] CGColor],
(id)[[UIColor colorWithWhite:1.0f alpha:0.1f] CGColor],
(id)[[UIColor clearColor] CGColor],
(id)[[UIColor colorWithWhite:0.0f alpha:0.1f] CGColor]];
_gradientLayer.locations = @[@0.00f, @0.01f, @0.95f, @1.00f];
[self.layer insertSublayer:_gradientLayer atIndex:0];
}
return self;
}
-(void) layoutSubviews {
[super layoutSubviews];
// ensure the gradient layers occupies the full bounds
_gradientLayer.frame = self.contentView.frame;
}
问题是,渐变没有覆盖我的附件视图占用的空间:
您还可以在左上角看到它也没有弯曲。
如何让渐变占据整个单元格空间?
编辑 - - -
如下所示,我添加了:
+(Class)layerClass
{
return [CAGradientLayer class];
}
并将单元格的 init 方法更改为:
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
_gradientLayer = (CAGradientLayer*)self.layer;
_gradientLayer.colors = @[(id)[[UIColor colorWithWhite:1.0f alpha:0.2f] CGColor],
(id)[[UIColor colorWithWhite:1.0f alpha:0.1f] CGColor],
(id)[[UIColor clearColor] CGColor],
(id)[[UIColor colorWithWhite:0.0f alpha:0.1f] CGColor]];
_gradientLayer.locations = @[@0.00f, @0.01f, @0.95f, @1.00f];
}
return self;
}
但是,现在渐变显示如下:
EDIT 2 --------- Tarks 下面的建议,在 UIView 中使用渐变然后分配给单元格的背景给出了这个结果!