Search code examples
objective-cuicollectionviewflowlayout

Warning in use custom collectionViewFlowLayot


I just modify the attributes in my 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;
}

Question like this, console logout in the first time of change the showed cell:

UICollectionViewFlowLayout has cached frame mismatch for index path {length = 2, path = 0 - 1} - cached value: {{350, 15.75}, {265, 283.5}}; expected value: {{350, 0}, {265, 315}}

This is likely occurring because the flow layout subclass MyFlowLayout is modifying attributes returned by UICollectionViewFlowLayout without copying them


Solution

  • Do like this, to copy the attributes

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

    This question has solved in this issue: Warning: UICollectionViewFlowLayout has cached frame mismatch for index path 'abc'