在 ViewController 出现之前加载数据/图形/背景的方法

Approach to load data / graphics / background before ViewController appears

我的 ViewController 视图中有一个动画背景,在这里调用它:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    gradientSet.append([gradientOne, gradientTwo])
    gradientSet.append([gradientTwo, gradientThree])
    gradientSet.append([gradientThree, gradientOne])

    gradient.frame = self.view.bounds
    gradient.colors = gradientSet[currentGradient]
    gradient.startPoint = CGPoint(x:0, y:0)
    gradient.endPoint = CGPoint(x:1, y:1)
    gradient.drawsAsynchronously = true
    viewBackground.layer.addSublayer(gradient)

    animateGradient()
}

这取决于加载时间,但是当我通过单击按钮加载 ViewController 时,我看到了 "white flashes" 的情况。我从来没有在专业应用程序上看到过这种行为,所以我认为我做的事情从根本上是错误的。

如何在 ViewController 显示加载时间之间没有白色闪烁的无缝过渡之前加载颜色、数据、背景内容?

将您的代码添加到 viewWillAppear 而不是 viewDidAppear

override func viewWillAppear(_ animated: Bool) {
    super. viewWillAppear(animated)

    gradientSet.append([gradientOne, gradientTwo])
    gradientSet.append([gradientTwo, gradientThree])
    gradientSet.append([gradientThree, gradientOne])

    gradient.frame = self.view.bounds
    gradient.colors = gradientSet[currentGradient]
    gradient.startPoint = CGPoint(x:0, y:0)
    gradient.endPoint = CGPoint(x:1, y:1)
    gradient.drawsAsynchronously = true
    viewBackground.layer.addSublayer(gradient)

    animateGradient()
}