Ios 滚动部分可见的上一项和下一项时,将集合视图居中

Ios 滚动部分可见的上一项和下一项时,将集合视图居中,ios,swift,collectionview,insets,Ios,Swift,Collectionview,Insets,我希望获得如下图所示的结果,这是我启动集合视图(第一项)时得到的结果: 但当我向左和向右滚动插图时,它们似乎不再相互尊重,项目也不再像这样居中: 我的代码部分: func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return

我希望获得如下图所示的结果,这是我启动集合视图(第一项)时得到的结果:

但当我向左和向右滚动插图时,它们似乎不再相互尊重,项目也不再像这样居中:

我的代码部分:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

return  CGSize(width: view.bounds.size.width - 60 , height: 450)
}


}

您好,我知道您要求每当集合视图稍微滚动时,您必须完全显示下一个单元格

要实现这一点,在collectionView中将显示单元格

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if !(indexPath.section == 0), !(indexPath.row == 0) {
    let indexPath = IndexPath(item: indexPath.row + 1, section: indexPath.section)
        self.collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: true)
 }
}
滚动到下一项


希望这对您有用:)

谢谢您的回复,但我上传的第二张图片显示了第二个项目的收藏视图。我没有像图中那样滚动一点。所以现在发生的事情是,当我滚动到另一个项目时,我没有完全看到该项目。我想完整地显示当前项目,同时也显示一点上一个项目和下一个项目,就像我上传的第一张图片一样
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if !(indexPath.section == 0), !(indexPath.row == 0) {
    let indexPath = IndexPath(item: indexPath.row + 1, section: indexPath.section)
        self.collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: true)
 }
}