使用自定义 collectionViewFlowLayot 时警告

Warning in use custom collectionViewFlowLayot

我只是修改了 flowLayout 中的属性

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSMutableArray *array = [[super layoutAttributesForElementsInRect:rect] mutableCopy];

    for (int i = 0; i< array.count; i++) {
        UICollectionViewLayoutAttributes* attributes = array[i];
        if (!attributes.representedElementKind) {
            array[i] = [self layoutAttributesForItemAtIndexPath:attributes.indexPath];
        }
    }
    return array;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForItemAtIndexPath:indexPath];
    CGFloat midX = self.collectionView.contentOffset.x + self.collectionView.width * 0.5;
    CGFloat distance = midX - attributes.center.x;
    CGFloat activeDistance = self.collectionView.width - lineSpacing - remainSpacing * 2;
    CGFloat normalizedDistance = distance / activeDistance;
    attributes.alpha = 1 - (1 - alphaFactor) * ABS(normalizedDistance);
    CGFloat zoom = 1 - (1 - scaleFactor) * ABS(normalizedDistance);
    attributes.transform3D = CATransform3DMakeScale(1.0, zoom, 1.0);
    attributes.zIndex = 1;
    return attributes;
}

这样的问题,第一次更改显示的单元格时控制台注销:

UICollectionViewFlowLayout 的缓存帧与索引路径 {length = 2, path = 0 - 1} 不匹配 - 缓存值:{{350, 15.75}, {265, 283.5}};期望值:{{350, 0}, {265, 315}}

这可能是因为流布局子类 MyFlowLayout 正在修改 UICollectionViewFlowLayout 返回的属性而不复制它们

这样做,复制属性

UICollectionViewLayoutAttributes *attributes = [[super layoutAttributesForItemAtIndexPath:indexPath] copy];

本期已解决此问题: