gpt4 book ai didi

ios - 向 UICollectionView 的第一个和最后一个单元格添加填充

转载 作者:技术小花猫 更新时间:2023-10-29 10:42:06 25 4
gpt4 key购买 nike

我已经将 UICollectionViewFlowLayout 子类化以获得具有类似分页行为的水平 UICollectionView。只要 UICollectionViewCell 不是最后一个单元格的第一个,它就可以正常工作。下面附上图片。

enter image description here enter image description here

除了跟随之外,我是否需要在我的 UICollectionViewFlowLayout 中覆盖某些东西?

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
CGFloat offSetAdjustment = MAXFLOAT;
CGFloat horizontalCenter = (CGFloat) (proposedContentOffset.x + (self.collectionView.bounds.size.width / 2.0));

CGRect targetRect = CGRectMake(proposedContentOffset.x,
0.0,
self.collectionView.bounds.size.width,
self.collectionView.bounds.size.height);

NSArray *array = [self layoutAttributesForElementsInRect:targetRect];
for (UICollectionViewLayoutAttributes *layoutAttributes in array)
{
if(layoutAttributes.representedElementCategory == UICollectionElementCategoryCell)
{
CGFloat itemHorizontalCenter = layoutAttributes.center.x;
if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offSetAdjustment))
{
offSetAdjustment = itemHorizontalCenter - horizontalCenter;
}
}
}

CGFloat nextOffset = proposedContentOffset.x + offSetAdjustment;

do {
proposedContentOffset.x = nextOffset;
CGFloat deltaX = proposedContentOffset.x - self.collectionView.contentOffset.x;
CGFloat velX = velocity.x;

if(deltaX == 0.0 || velX == 0 || (velX > 0.0 && deltaX > 0.0) || (velX < 0.0 && deltaX < 0.0))
{
break;
}

if(velocity.x > 0.0)
{
nextOffset += [self snapStep];
}
else if(velocity.x < 0.0)
{
nextOffset -= [self snapStep];
}
} while ([self isValidOffset:nextOffset]);

proposedContentOffset.y = 0.0;

return proposedContentOffset;
}
- (BOOL)isValidOffset:(CGFloat)offset
{
return (offset >= [self minContentOffset] && offset <= [self maxContentOffset]);
}

- (CGFloat)minContentOffset
{
return -self.collectionView.contentInset.left;
}

- (CGFloat)maxContentOffset
{
return [self minContentOffset] + self.collectionView.contentSize.width - self.itemSize.width;
}

- (CGFloat)snapStep
{
return self.itemSize.width + self.minimumLineSpacing;
}

任何指针/评论都会很有用。

最佳答案

在设置 Collection View 的框架时,您可以将左侧和右侧的空间设置为等于您的填充。

您可以在 cellForItemAtIndexPath 中放置条件,如果它是第一个单元格或最后一个单元格,则相应地管理填充。就是这样。

或者

您可以设置 collectionView 的 contentInset 属性。

例如,

UICollectionView *cv; // your collectionView

cv.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);

或者,您可以在 Storyboard中设置 UICollectionView contentInset 以使其正常工作。

关于ios - 向 UICollectionView 的第一个和最后一个单元格添加填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39117231/

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