选项卡视图控制器,集合视图中的scrollToItem在切换选项卡后不起作用

时间:2017-09-17 13:41:49

标签: ios swift uicollectionview

我有一个标签栏视图控制器。主视图控制器具有集合视图。我想动画一个包含在数组中的单元格列表,如下所示:

// 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中的索引路径。这一切都很好,但如果我中断进程并切换选项卡并返回到视图控制器,动画将不会继续。它调用collectionView.scrollToItem(at: indexPath, at: .bottom, animated: true)但它没有动画,并且没有调用委托。但是,如果我用手指轻轻滚动桌子并离开并返回到屏幕,它将恢复动画而没有任何问题。

这里有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为这是因为当您切换视图控制器时,集合视图不能以编程方式滚动。

尝试添加此内容:

.XYZ {
    width : 10character;
}

这样,当您返回此视图控制器时,您将回滚到override func viewWillDisappear(_ animated: Bool) { if let indexPath = newAchievementIndexPaths[1] { collectionView.scrollToItem(at: indexPath, at: .bottom, animated: true) } } 中的该项目。