在启用本机分页的情况下查看 UICollectionView 中的 previous/next 个单元格?

Peek previous/next cells in UICollectionView with native paging enabled?

我希望集合视图通过单元格分页并居中,但像这样显示上一个和下一个单元格的一部分:

那里有很多技巧,但我想通过 UICollectionView 的本机分页 属性 来实现。使单元格成为集合视图的整个宽度不会显示 previous/next 个单元格,并且使单元格宽度变小不会在分页时对齐到中心。

例如,是否可以使集合视图占屏幕宽度的 80%,并让 previous/next 单元格溢出到边界之外(没有裁剪到边界)?

或者使用本机分页实现此目的的任何其他想法?

我认为在启用本机分页的情况下没有一种简单的方法可以做到这一点。 但是您可以通过使用 scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) 设置您想要的目的地轻松地进行自定义寻呼。通过向其中添加一些逻辑,您可以创建分页。 您可以找到示例 here and here

iOS Swift 4

使用以下两种方法来调和前后画面。

private func calculateSectionInset() -> CGFloat {
    let deviceIsIpad = UIDevice.current.userInterfaceIdiom == .pad
    let deviceOrientationIsLandscape = UIDevice.current.orientation.isLandscape
    let cellBodyViewIsExpended = deviceIsIpad || deviceOrientationIsLandscape
    let cellBodyWidth: CGFloat = 236 + (cellBodyViewIsExpended ? 174 : 0)
    let buttonWidth: CGFloat = 50
    let inset = (collectionFlowLayout.collectionView!.frame.width - cellBodyWidth + buttonWidth) / 4
    return inset
}

private func configureCollectionViewLayoutItemSize() {
    let inset: CGFloat = calculateSectionInset() // This inset calculation is some magic so the next and the previous cells will peek from the sides. Don't worry about it
    collectionFlowLayout.sectionInset = UIEdgeInsets(top: 0, left: inset, bottom: 0, right: inset)
    collectionFlowLayout.itemSize = CGSize(width: collectionFlowLayout.collectionView!.frame.size.width - inset * 2, height: collectionFlowLayout.collectionView!.frame.size.height)
}

不要忘记在 UIViewControllerviewDidLayoutSubviews() 中调用 configureCollectionViewLayoutItemSize() 方法。

更详细的参考Click Here