ios – 使用CustomFlowLayout的UICollectionView如何将滚动限制为每个滚动只有一个页面?

ios – 使用CustomFlowLayout的UICollectionView如何将滚动限制为每个滚动只有一个页面?,第1张

概述我在iOS App中实现了customFlowLayout.我已经将targetContentOffsetForProposedContentOffset子类化了:withScrollingVelocity with  子类化UICollectionViewFlowLayout.现在我的问题是当用户滚动集合视图时,它必须滚动到只有下一个索引.现在它随机滚动. 所以任何人都知道如何使滚动限制为每个滚 我在iOS App中实现了customFlowLayout.我已经将targetContentOffsetForProposedContentOffset子类化了:withScrollingVeLocity with
 子类化UICollectionVIEwFlowLayout.现在我的问题是当用户滚动集合视图时,它必须滚动到只有下一个索引.现在它随机滚动.
所以任何人都知道如何使滚动限制为每个滚动只有一个项目.
以下是我的代码.

#pragma mark - UICollectionVIEwLayout (UISubclassingHooks)- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVeLocity:(CGPoint)veLocity {  CGSize collectionVIEwSize = self.collectionVIEw.bounds.size;  CGfloat proposedContentOffsetCenterX = proposedContentOffset.x + collectionVIEwSize.wIDth / 2;  CGRect proposedRect = CGRectMake(proposedContentOffset.x,collectionVIEwSize.wIDth,collectionVIEwSize.height);  UICollectionVIEwLayoutAttributes *candIDateAttributes;  for (UICollectionVIEwLayoutAttributes *attributes in [self layoutAttributesForElementsInRect:proposedRect]) {    if (attributes.representedElementcategory != UICollectionElementcategoryCell) continue;    if (!candIDateAttributes) {      candIDateAttributes = attributes;      continue;    }    if (fabs(attributes.center.x - proposedContentOffsetCenterX) < fabs(candIDateAttributes.center.x - proposedContentOffsetCenterX)) {      candIDateAttributes = attributes;    }  }  proposedContentOffset.x = candIDateAttributes.center.x - self.collectionVIEw.bounds.size.wIDth / 2;  CGfloat offset = proposedContentOffset.x - self.collectionVIEw.contentOffset.x;  if ((veLocity.x < 0 && offset > 0) || (veLocity.x > 0 && offset < 0)) {    CGfloat pageWIDth = self.itemSize.wIDth + self.minimumlinespacing;    proposedContentOffset.x += veLocity.x > 0 ? pageWIDth : -pageWIDth;  }  return proposedContentOffset;}- (BOol)shouldInvalIDateLayoutForBoundsChange:(CGRect)newBounds {  return YES;}- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {  if (!self.scaleItems) return [super layoutAttributesForElementsInRect:rect];  NSArray *attributesArray = [[NSArray alloc] initWithArray:[super layoutAttributesForElementsInRect:rect] copyItems:YES];  CGRect visibleRect = (CGRect){self.collectionVIEw.contentOffset,self.collectionVIEw.bounds.size};  CGfloat visibleCenterX = CGRectGetMIDX(visibleRect);  [attributesArray enumerateObjectsUsingBlock:^(UICollectionVIEwLayoutAttributes *attributes,NSUInteger IDx,BOol *stop) {    CGfloat distanceFromCenter = visibleCenterX - attributes.center.x;    CGfloat absdistanceFromCenter = MIN(ABS(distanceFromCenter),self.scalingOffset);    CGfloat scale = absdistanceFromCenter * (self.minimumScaleFactor - 1) / self.scalingOffset + 1;    attributes.transform3D = CAtransform3DScale(CAtransform3DIDentity,scale,1);  }];  return attributesArray;}
解决方法 您的代码看起来应该根据用户请求很好地滚动.即如果它们快速滚动它将跳过许多项目并很好地落在后面的项目上,如果它们慢慢滚动它将继续下一个或根据滚动距离很好地返回到前一项目.但是,这不是你想要的.

当用户试图快速滚动时,你想要的东西可能不是很好用…

无论如何,为了获得你想要的东西,你基本上只想使用proposedContentOffset来确定滚动方向(它是否大于或小于当前内容偏移量).

现在,一旦你有了,你就可以获得下一页或上一页的项目的布局属性(而不是当前的代码可能会获得许多页面的属性).这是当前偏移量或 – 视图宽度.

其余的代码保持不变.忽略滚动方向,这类似于:

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVeLocity:(CGPoint)veLocity{    CGSize collectionVIEwSize = self.collectionVIEw.bounds.size;    CGfloat wIDth = collectionVIEwSize.wIDth;    CGfloat halfWIDth = wIDth * 0.5;    CGfloat direction = (proposedContentOffset.x > self.collectionVIEw.contentOffset.x ? 1 : 0);    CGfloat pageOffsetX = 250.0 * floor(self.collectionVIEw.contentOffset.x / 250.0);     CGfloat proposedContentOffsetCenterX = pageOffsetX + (wIDth * direction);    CGRect proposedRect = CGRectMake(proposedContentOffsetCenterX,collectionVIEwSize.height);    UICollectionVIEwLayoutAttributes *candIDateAttributes;    for (UICollectionVIEwLayoutAttributes *attributes in [self layoutAttributesForElementsInRect:proposedRect]) {        if (attributes.representedElementcategory != UICollectionElementcategoryCell) continue;        candIDateAttributes = attributes;        break;    }    proposedContentOffset.x = candIDateAttributes.center.x - halfWIDth;//     CGfloat offset = proposedContentOffset.x - self.collectionVIEw.contentOffset.x;//     //     if ((veLocity.x < 0 && offset > 0) || (veLocity.x > 0 && offset < 0)) {//         CGfloat pageWIDth = self.itemSize.wIDth + self.minimumlinespacing;//         proposedContentOffset.x += veLocity.x > 0 ? pageWIDth : -pageWIDth;//     }    return proposedContentOffset;}

我已经在底部注释掉了部分,因为初始版本不需要它.首先使用简单版本进行测试,然后详细说明是否需要在边缘情况下进行更多控制.

总结

以上是内存溢出为你收集整理的ios – 使用CustomFlowLayout的UICollectionView如何将滚动限制为每个滚动只有一个页面?全部内容,希望文章能够帮你解决ios – 使用CustomFlowLayout的UICollectionView如何将滚动限制为每个滚动只有一个页面?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1029514.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存