2

我刚刚注意到 uicollectionviewcells 的中心是真实中心的一半。

例子:

将单元格设置为:

[0] {{0, 0}, {100, 100}}
[1] {{100, 100}, {100, 100}}
[2] {{200, 200}, {100, 100}}
[3] {{300, 300}, {100, 100}}
[4] {{400, 400}, {100, 100}}
[5] {{500, 500}, {100, 100}}

注意:这是我从 setFrame 和applyLayoutAttributes 获得的值:

看起来像这样: 实际外观

但应该是这样的: 它应该是什么样子

UICollectionViewLayout 子类的实现

@implementation CVTLayout

- (void)prepareLayout {
    [super prepareLayout];

    CollectionViewTable *cvt = (CollectionViewTable*)self.collectionView;


    CGSize size = CGSizeMake(100, 100);
    CGPoint center = CGPointMake(50, 50);
    NSMutableArray *cellAttributes = [[NSMutableArray alloc] init];
    for (int item = 0; item < [cvt.cellValues count]; item++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:0];
        UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        attributes.size = size;
        attributes.center = center;
        [cellAttributes addObject:attributes];

        center.x += size.width;
        center.y += size.height;
    }
    cvt.cellAttributes = cellAttributes;
}

- (CGSize)collectionViewContentSize {
    return self.collectionView.bounds.size;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewTable *cvt = (CollectionViewTable*)self.collectionView;
    return cvt.cellAttributes[indexPath.item];
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSMutableArray *layoutAttributes = [[NSMutableArray alloc] init];
    CollectionViewTable *cvt = (CollectionViewTable*)self.collectionView;
    for (UICollectionViewLayoutAttributes *cellAttributes in cvt.cellAttributes) {
        if (CGRectIntersectsRect(rect, cellAttributes.frame)) {
            [layoutAttributes addObject:cellAttributes];
        }
    }
    return layoutAttributes;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return false;
}

@end

4

1 回答 1

1

问题是将 cell-label-frame 设置为 cell-frame 而不是 cell-bounds ...

于 2013-06-07T09:01:17.890 回答