gpt4 book ai didi

ios - UICollectionViewCell 位置加倍

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:42:44 27 4
gpt4 key购买 nike

我刚刚注意到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: 获得的值

看起来像这样: actual look

但应该是这样的: how it should look

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

最佳答案

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

关于ios - UICollectionViewCell 位置加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16961009/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com