【问题标题】:Snap UICollection View Cell to centre using Objective C使用 Objective C 将集合视图单元格对齐到中心
【发布时间】:2017-11-14 14:46:58
【问题描述】:

我正在 Objective C 中实现一个水平 UICollectionView。我想将单元格捕捉到集合视图的中心,如下图所示。当一个项目被捕捉到中心时,我希望左侧和右侧的项目部分可见。我怎样才能做到这一点?

我使用了自定义 IOCOllectionViewFlowLayout 并覆盖了

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity

但它不适合我,

CGFloat approximatePage = self.collectionView.contentOffset.x / self.pageWidth;
    CGFloat currentPage = (velocity.x < 0.0) ? floor(approximatePage) : ceil(approximatePage);

    NSInteger flickedPages = ceil(velocity.x / self.flickVelocity);
    float pageWidth = self.pageWidth;
    if (flickedPages) {
        proposedContentOffset.x = flickedPages * self.pageWidth;
    } else {
        proposedContentOffset.x = currentPage * self.pageWidth;
    }
    return proposedContentOffset;

如何使用 Objective C 来实现它?

【问题讨论】:

  • 你的意思是点击想要的单元格?
  • 不,滚动时它应该以该单元格为中心。
  • 你的意思是当用户停止滚动,而滚动是不好的用户体验。

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout


【解决方案1】:

您需要使用 UIViewController 滚动视图的 scrollViewWillEndDragging 委托进行操作。进一步阅读这里https://rolandleth.com/uicollectionview-snap-scrolling-and-pagination

【讨论】:

    【解决方案2】:

    在这种情况下,当您需要水平居中时,您需要实现委托方法 scrollViewWillEndDragging,正如 kirander 所提到的。当您检测到用户停止滚动时,检测可见单元格并将其放在中心并使用以下代码:projectSelectionCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      相关资源
      最近更新 更多