ios - 如何修复 UICollectionViewFlowLayout Transition 未将样式应用于单元格?

标签 ios uicollectionview uicollectionviewcell uicollectionviewlayout

我在这里创建了一个带有布局转换的非常基本的 UICollectionView: https://github.com/aubrey/TestCollectionView

这里有一个关于我的问题的视频: 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;
}

最佳答案

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

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

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

您确实遇到了 Apple 工程师可能没有预见到的交叉问题。我不相信你有很多选择。覆盖 applyLayoutAttributes: 并摆弄显式动画可能会奏效,但有我之前提到的限制。您最好的选择可能是创建一个可调整大小的 UIImage 来表示您的影子,然后将 UIImageView 添加到单元格的 View 层次结构中,以便随着单元格的增大和缩小用阴影做 ImageView 。我知道从代码的角度来看,这不是一个令人满意的答案,但它是最通用的答案,会导致最少的挫败感。

关于ios - 如何修复 UICollectionViewFlowLayout Transition 未将样式应用于单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25579720/

相关文章:

swift - 如何滚动循环CollectionView

scroll - UICollectionView有效拖放

swift - UICollectionView - 水平表格 View - Swift - 在每一行中显示不同的图像

ios - UICollectionView 重叠标签

ios - 动画图像动画RepeatCount CollectionViewCell

ios - 在 iOS 9 的多个 WKWebView 中同步 cookie

ios - 无法在服务器(SpringBoot)和 iOS 之间建立 WebSocket 连接

iphone - Xcode 4 - viewDidLoad 问题

swift - 如何将特定内容从 Collection View 添加到另一个 Collection View ?

ios - 如何在我的情况下创建 ViewController? IOS 7