我刚注意到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:
看起来像这样:
但应该是这样的:
@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
答案 0 :(得分:1)
问题是将cell-label-frame设置为cell-frame而不是cell-bounds ...