如何滚动到 UICollectionView 的中心(水平和垂直)

Posted

技术标签:

【中文标题】如何滚动到 UICollectionView 的中心(水平和垂直)【英文标题】:How to scroll to the center of UICollectionView (Both Horizontally and Vertically) 【发布时间】:2019-03-05 10:04:20 【问题描述】:

我已经实现了一个可以水平和垂直滚动的collectionview(基本上它是一个网格)。我希望能够在垂直和水平动画选择项目时滚动到项目。

目前我将其用作“解决方法”,但由于显而易见的原因,它仍然会跳转。

 self.scrollToItem(at: indexPath, at: .centeredVertically, animated: false)
 self.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)

有没有办法实现我想要发生的事情?

【问题讨论】:

【参考方案1】:

UICollectionView.ScrollPosition 是一个OptionSet

你试过了吗

scrollToItem(at: indexPath, at: [.centeredVertically, .centeredHorizontally], animated: false)

?

【讨论】:

谢谢!是的,这正是我所需要的。

以上是关于如何滚动到 UICollectionView 的中心(水平和垂直)的主要内容,如果未能解决你的问题,请参考以下文章