我正在使用此代码在集合视图中显示特定项目。
var pages: [PDFPage]?
var currentPage: PDFPage?
override func viewDidLayoutSubviews() {
// Scroll to currently opened page
if let current = currentPage, let idx = pages?.firstIndex(of: current) {
collectionView?.scrollToItem(at: IndexPath(item: idx, section: 0), at: .centeredVertically, animated: false)
}
}
它正在按预期方式工作,但它完全锁定了用户滚动,以便始终将项目保持在请求的位置。 使用scrollRectToVisible时也会发生同样的情况。 相反,我希望它仅移至该项目,然后让用户滚动至所需位置。我通过UITableViewController的scrollToRow得到的行为。
我想念什么吗?
答案 0 :(得分:0)
事实证明,调用viewDidLayoutSubviews中的滚动是一个问题,因为它不断重复并阻止了任何进一步的滚动。我这样解决了这个问题:
var currentPage: PDFPage?
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Scroll to currently opened outline
if let current = currentPage, let idx = pages?.firstIndex(of: current) {
collectionView?.scrollToItem(at: IndexPath(item: idx, section: 0),
at: .top,
animated: false)
// In order to only run the scroll once, without locking the view
currentPage = nil
}
}