gpt4 book ai didi

ios - 选项卡式 View Controller ,切换选项卡后 Collection View 中的scrollToItem不起作用

转载 作者:行者123 更新时间:2023-11-30 12:09:19 25 4
gpt4 key购买 nike

我有一个选项卡式栏 View Controller 。主视图 Controller 有一个 Collection View 。我想对数组中包含的单元格列表进行动画处理,如下所示:

// Called in viewDidAppear
func animateNewAchievements() {
if let indexPath = newAchievementIndexPaths.first {
collectionView.scrollToItem(at: indexPath, at: .bottom, animated: true)
}
}
// Delegate method
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
if let indexPath = newAchievementIndexPaths.first {
if let cell = collectionView.cellForItem(at: indexPath) as? TrophyCollectionViewCell {
cell.animate(completion: {
if self.newAchievementIndexPaths.count > 0 {
self.newAchievementIndexPaths.removeFirst()
}
self.perform(#selector(self.animateNewAchievements), with: nil, afterDelay: 0.4)
})
}
}
}

如您所见,是否将一一循环遍历 newAchievementIndexPaths 中的索引路径。这一切都运行得很好,但是如果我中断进程并切换选项卡并返回到 View Controller ,动画将不会继续。它调用collectionView.scrollToItem(at:indexPath, at:.bottom,animated:true)但它不会动画并且不会调用委托(delegate)。但是,如果我用手指轻轻滚动表格并离开并返回屏幕,它将恢复动画而不会出现任何问题。

知道这里发生了什么吗?

最佳答案

我认为这是因为当您切换 View Controller 时 Collection View 不会以编程方式滚动。

尝试添加以下内容:

override func viewWillDisappear(_ animated: Bool) {
if let indexPath = newAchievementIndexPaths[1] {
collectionView.scrollToItem(at: indexPath, at: .bottom, animated: true)
}
}

这样,当您返回到此 View Controller 时,您就可以滚动回 viewDidAppear 中的该项目。

关于ios - 选项卡式 View Controller ,切换选项卡后 Collection View 中的scrollToItem不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46264720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com