CollectionView戏弄前一个和下一个

Pie*_*rre 6 objective-c ios uicollectionview uicollectionviewlayout

我想,我的问题有点简单,但看不到如何实现这一目标.

我想要一个横向UICollectionView,我可以看到我的上一个和下一个单元格.
我已经设置了水平,分页启用collectionView及其流程布局.
我"只是"想念这件事.

这是我想要的模型.

在此输入图像描述

我的问题是:我无法得到这个位置:当前单元格居中,前一个和下一个出现.我没有成功地使用我的布局- minimumInteritemSpacing- sectionInset属性.

Ric*_*hiy 0

这是以下布局的代码:

import UIKit

/**
 A UICollectionViewFlowLayout subclass that snaps to the nearest cell as soon as the user stops dragging.
 Respects the scrolling velocity.
 Only _Horizontal_ direction is supported.
 */
class SnapCenterLayout: UICollectionViewFlowLayout {
  override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
    guard let collectionView = collectionView else { return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity) }
    let parent = super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity)

    let itemSpace = itemSize.width + minimumInteritemSpacing
    var currentItemIdx = round(collectionView.contentOffset.x / itemSpace)

    // Skip to the next cell, if there is residual scrolling velocity left.
    // This helps to prevent glitches
    let vX = velocity.x
    if vX > 0 {
      currentItemIdx += 1
    } else if vX < 0 {
      currentItemIdx -= 1
    }

    let nearestPageOffset = currentItemIdx * itemSpace
    return CGPoint(x: nearestPageOffset,
                   y: parent.y)
  }
}
Run Code Online (Sandbox Code Playgroud)