【发布时间】:2014-11-17 12:44:17
【问题描述】:
我必须在一些自定义UICollectionViewCells 的背景图像中添加一个CAGradientLayer。我正在使用显示这样的单元格的自定义流布局。
当我滚动collectionView 并且添加到图像的渐变层改变了它们的大小时,我遇到了可重用性问题。正如你在这张图片中看到的,渐变改变了它的大小。
我也有一些单元格的问题,这些单元格添加了两次渐变层。所以我要加的阴影效果太暗了。
我正在使用此代码。
- (void)prepareForReuse {
[super prepareForReuse];
[self.gradientLayer removeFromSuperlayer];
self.gradientLayer = nil;
self.articleImageView.image = nil;
}
- (void)layoutSubviews {
[super layoutSubviews];
[self setupGradient];
}
#pragma mark - Setup
- (void)setupGradient {
UIColor *threeQuartersBlack = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
UIColor *halfBlack = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
UIColor *oneQuarterBlack = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
CAGradientLayer *gradient = [CAGradientLayer layer];
CGFloat gradientHeight =self.bounds.size.height/3.0;
CGRect gradientBounds = CGRectMake(0, self.bounds.size.height-gradientHeight, self.bounds.size.width, gradientHeight);
gradient.frame = gradientBounds;
gradient.anchorPoint = CGPointMake(0.5, 0.5);
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor],(id)oneQuarterBlack.CGColor ,(id)halfBlack.CGColor , (id)threeQueartesBlack.CGColor, (id)[[UIColor blackColor] CGColor], nil];
self.gradientLayer = gradient;
[self.articleImageView.layer insertSublayer:self.gradientLayer atIndex:0];
}
我注意到如果我弹出那个 ViewController 并再次显示它,一切都是正确的。
有人知道我应该怎么做才能解决这个问题吗?
谢谢
【问题讨论】:
-
你明白你的问题解决了吗?我正是你的问题...
标签: ios objective-c uicollectionview calayer