Swift 如何通过滚动滑块来滚动 UICollectionView?

How to scroll UICollectionView by scrolling slider in Swift?

我想让 collectionView 在用户滚动 UISlider 时滚动。我正在使用具有与集合相同数量的值的 UISlider。但是不是很顺利。我的代码:

let currentValue = Int(sender.value)
collectionView.scrollToItem(at: IndexPath(row: currentValue, section: 0), at: .top, animated: true)

也许有更有效的方法来做到这一点?喜欢按偏移量滚动吗?但是我不知道如何使用方法 collectionView.setContentOffset

计算值来做到这一点

你可以试试:

self.collectionView.contentOffset.y += sender.value // sender is your UISlider