Ios 如何修复未将样式应用于单元格的UICollectionViewFlowLayout转换?

Ios 如何修复未将样式应用于单元格的UICollectionViewFlowLayout转换?,ios,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Ios,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,我在这里创建了一个非常基本的UICollectionView,其中包含布局转换: 这里有一个关于我的问题的视频: 我的问题是我不知道在哪里/如何将我添加的阴影应用到单元格中。每当我添加它时,它都不会正确地应用于转换的单元格,并在我返回转换后挂起 在我的didSelectItemAtIndexPath方法中,我尝试在此处应用阴影(无效): 我还在设置自定义单元格时应用了阴影: @implementation MyCell - (id)initWithFrame:(CGRect)frame { s

我在这里创建了一个非常基本的UICollectionView,其中包含布局转换:

这里有一个关于我的问题的视频:

我的问题是我不知道在哪里/如何将我添加的阴影应用到单元格中。每当我添加它时,它都不会正确地应用于转换的单元格,并在我返回转换后挂起

在我的didSelectItemAtIndexPath方法中,我尝试在此处应用阴影(无效):

我还在设置自定义单元格时应用了阴影:

@implementation MyCell

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

    self.contentView.backgroundColor = [UIColor whiteColor];

    self.myNumber = [UILabel new];
    self.myNumber.text = @"Data Array Didn't Load";
    self.myNumber.frame = CGRectMake(20, 20, 100, 100);
    [self.contentView addSubview:self.myNumber];

//       Shadow Setup
        self.layer.masksToBounds = NO;
        self.layer.shadowOpacity = 0.15f;
        self.layer.shadowRadius = 1.4f;
        self.layer.shadowOffset = CGSizeZero;
        self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;

}
return self;
}

有趣的问题——阴影似乎总是会引起问题,不是吗?如果我理解正确的话,问题不是阴影没有出现,而是阴影没有考虑单元格的新边界

通常,将此类自定义属性应用于单元格的最佳位置是覆盖
applyLayoutAttributes:
。然而,在这种情况下,这将是棘手的。这是因为,与应用属于UIKit的隐式可设置动画的属性不同,阴影设置在单元的CALayer上,这意味着要获得阴影的动画,您可能需要显式的
CAAnimation

使用显式动画的问题在于,无法在运行时确定动画的持续时间。此外,假设您希望在没有动画的情况下从一个布局过渡到另一个布局。UICollectionView API中没有处理该问题的工具

你真的遇到了苹果工程师可能没有预见到的问题。我不相信你有很多选择。重写
applyLayoutAttributes:
和摆弄显式动画可能会起作用,但有我前面提到的限制。最好的方法可能是创建一个可调整大小的表示阴影的
UIImage
,然后将
UIImageView
添加到单元格的视图层次结构中,以便随着单元格的增长和收缩,带阴影的图像视图也会随之增长和收缩。我知道,从代码的角度来看,这不是一个令人满意的答案,但它是最通用的答案,将导致最少的挫折

@implementation MyCell

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

    self.contentView.backgroundColor = [UIColor whiteColor];

    self.myNumber = [UILabel new];
    self.myNumber.text = @"Data Array Didn't Load";
    self.myNumber.frame = CGRectMake(20, 20, 100, 100);
    [self.contentView addSubview:self.myNumber];

//       Shadow Setup
        self.layer.masksToBounds = NO;
        self.layer.shadowOpacity = 0.15f;
        self.layer.shadowRadius = 1.4f;
        self.layer.shadowOffset = CGSizeZero;
        self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;

}
return self;
}