0

我有一个 UICollectionView 和一个 customCell,它有一个 UIImageView 我正在添加阴影。但是,在单元格“超出”可见集合视图并返回 (UICollection) 视图之前,不会绘制阴影。

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier = @"genreCell";

GenreViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

cell.genreImageView.layer.shadowRadius = 1.5;
cell.genreImageView.layer.shadowOffset = CGSizeMake(-1, -1);
cell.genreImageView.layer.shadowOpacity = 0.5;
cell.genreImageView.layer.shadowColor = [UIColor blackColor].CGColor;

cell.genreImageView.layer.shadowPath = [UIBezierPath bezierPathWithRect:cell.genreImageView.bounds].CGPath;

return cell;

}
4

2 回答 2

3

Is there a reason you are setting the shadowPath property? Since you want to set the shadow to the bounds of the image view itself, you shouldn't have to do that. Remove that line.

Also, to prevent lag:

cell.genreImageView.layer.shouldRasterize = YES;
cell.genreImageView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
于 2013-05-14T21:03:07.930 回答
3

第一次cellForItemAtIndexPath调用方法时未设置单元格的边界。因此shadowPath计算不正确。确保每当单元格的边界发生变化时更新 shadowPath。layoutSubviews的方法GenreViewCell可以用于此。

于 2013-05-14T20:59:54.707 回答