如何移动到 UiCollectionView 中的选定单元格

How to move to the selected cell in UiCollectionView

我是Swift的新手,请多多包涵

我创建了一个水平滚动的 UICollectionView,我的问题是:如何以编程方式移动到选定的图标?

我已经用过didSelectedPath,接下来呢?

您需要调用scrollToItemAtIndexPath函数:

Scrolls the collection view contents until the specified item is visible.

 func scrollToItemAtIndexPath(_ indexPath: NSIndexPath,
                atScrollPosition scrollPosition: UICollectionViewScrollPosition,
                        animated animated: Bool)

参数

indexPath

要滚动到视图中的项目的索引路径。

scrollPosition

一个选项,用于指定滚动完成时项目应放置的位置。有关可能值的列表,请参阅 UICollectionViewScrollPosition

animated

指定 true 以动画滚动行为或 false 立即调整滚动视图的可见内容。

查看 UICollectionView class 参考以获取更多信息。