1

我创建了一个非常基本的UICollectionView与布局过渡位置:https://github.com/aubrey/TestCollectionView如何修复UICollectionViewFlowLayout不将样式应用于单元格?

这里有我的问题的视频:http://cl.ly/XHjZ

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

在我didSelectItemAtIndexPath方法我试图在这里将阴影(无济于事):

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 

if (self.collectionView.collectionViewLayout == self.smallLayout) 
{ 
    [self.largeLayout invalidateLayout]; 
    [self.collectionView setCollectionViewLayout:self.largeLayout animated:YES]; 
    [self.collectionView setPagingEnabled:YES]; 
} 

else 
{ 
    [self.smallLayout invalidateLayout]; 
    [self.collectionView setCollectionViewLayout:self.smallLayout animated:YES]; 
    [self.collectionView setPagingEnabled:NO]; 

} 
} 

我还申请了影子在那里我建立我的自定义单元格:

@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; 
} 

回答

1

有趣的问题 - 阴影总是会引起问题,不是吗?如果我理解正确,问题不在于影子没有出现,而在于影子不在尊重细胞的新界限。

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

使用显式动画的问题在于无法在运行时确定动画的持续时间。另外,假设你想从一个布局转换到另一个布局,而不需要动画。 UICollectionView API中没有设施来处理这个问题。

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

相关问题