ios - UICollectionView 和 CustomFlowLayout 如何将滚动限制为每次滚动仅一页?

标签 ios objective-c scroll uicollectionview scroll-paging

我在我的 iOS 应用程序中实现了 customFlowLayout。我已经将 targetContentOffsetForProposedContentOffset:withScrollingVelocity 子类化为 子类化 UICollectionViewFlowLayout。现在我的问题是,当用户滚动 collectionview 时,它必须只滚动到下一个索引。现在它随机滚动。
所以任何人都知道如何将滚动限制为每个滚动只有一个项目。
以下是我的代码。

#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, 0, 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, scale, 1);
  }];

  return attributesArray;
}

最佳答案

您的代码看起来应该根据用户请求很好地滚动。也就是说,如果他们快速滚动,它将跳过一些项目并很好地落在后面的项目上,如果他们滚动缓慢,它将继续到下一个或返回到上一个项目,具体取决于滚动距离。但是,这不是您所说的。

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

无论如何,要获得您想要的内容,您基本上只想使用 proposedContentOffset 来确定滚动方向(它是大于还是小于当前内容偏移量)。

现在,一旦你有了它,你就可以在下一页或上一页获取项目的布局属性(而不是当前代码可能会在许多页面之外获取属性)。这是当前偏移量 + 或 - View 宽度。

您的其余代码将保持不变。忽略滚动方向是这样的:

- (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, 0, collectionViewSize.width, 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 - UICollectionView 和 CustomFlowLayout 如何将滚动限制为每次滚动仅一页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35980582/

相关文章:

html - 是否可以让 child 漂浮在垂直滚动条上?

iphone - 实现授权

ios - 如何删除 UICollectionView 中单元格的空间

iOS - 根据 TableView 选定的行自定义 segue ViewController 的最简单方法是什么?

ios - 在 swift ios 中使用 webkitview

objective-c - 在tableView中删除后崩溃并弹出动画 View 。另一个iOS7 SDK错误?

objective-c - 在 iOS7 中更改 MFMailComposeViewController 的 UINavigationBar 颜色

Jquery DIV 元素上下滚动?

ios - 单击时将 UiScrollView 元素置于中心

ios - 如何在 UIWebView 中使用 Apple(应用内购买)?