Swift UIApplicationEndBackgroundTaskError与CAAnimation

Swift UIApplicationEndBackgroundTaskError与CAAnimation,swift,background,swiftui,Swift,Background,Swiftui,我正在应用程序中异步运行一个动画(动画渐变)作为背景图像。单击主页按钮(转到主屏幕,不关闭应用程序),然后重新进入应用程序时,出现UIApplicationEndBackgroundTaskError。如果有人能弄明白这一点,我将不胜感激。此错误的一个副作用是动画停止播放/暂停。谢谢(请查看下面的详细信息) 我使用符号中断来查看错误,结果如下所示: 背景代码在此处(在SwiftUI视图中使用)为GradientView(): struct GradientView:UIViewControll

我正在应用程序中异步运行一个动画(动画渐变)作为背景图像。单击主页按钮(转到主屏幕,不关闭应用程序),然后重新进入应用程序时,出现UIApplicationEndBackgroundTaskError。如果有人能弄明白这一点,我将不胜感激。此错误的一个副作用是动画停止播放/暂停。谢谢(请查看下面的详细信息)

我使用符号中断来查看错误,结果如下所示:

背景代码在此处(在SwiftUI视图中使用)为GradientView():

struct GradientView:UIViewControllerRepresentable{
typealias UIViewControllerType=GradientBackground
func makeUIViewController(上下文:UIViewControllerRepresentableContext)->GradientView.UIViewControllerType{
return GradientBackground()
}
func updateUIViewController(uViewController:GradientView.UIViewControllerType,上下文:UIViewControllerRepresentableContext){
}
}
类GradientBackground:UIViewController{
设梯度=CAGradientLayer()
var gradientSet=[[CGColor]]()
var currentGradient=0
让gradOne=#colorLiteral(红色:0.4620226622,绿色:0.8382837176,蓝色:1,阿尔法:1)
设gradtow2=#colorLiteral(红色:1,绿色:0.832345645,蓝色:0.4732058644,阿尔法:1)
设gradtree=#colorLiteral(红色:0,绿色:0.3285208941,蓝色:0.5748849511,阿尔法:1)
覆盖函数视图显示(u动画:Bool){
super.viewdide显示(动画)
animatedGradient()
}
重写func viewDidLoad(){
super.viewDidLoad()
gradientSet.append([gradOne,gradTwo])
追加([gradTwo,gradTwo])
gradientSet.append([gradThree,gradOne])
gradient.startPoint=CGPoint(x:0,y:0)
gradient.endPoint=CGPoint(x:1.0,y:1.0)
gradient.drawsa同步=真
//gradient.colors=[渐变一、渐变二]
view.layer.addSublayer(渐变)
}
重写func viewdilayoutsubviews(){
gradient.frame=view.bounds
}
func animatedgradent(){
变量prevGrad:Int!
如果currentGradient
我可能错了,但记录的错误看起来像是苹果的问题。至于在应用程序重新激活时恢复动画,您可以尝试下面我在测试中使用的代码,这很有效

import Combine

...

class GradientBackground : UIViewController {

...
private var subscribers = [AnyCancellable]()

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

    NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification)
        .sink { _ in
            self.gradient.removeAllAnimations()
        }.store(in: &subscribers)
    NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
        .sink { _ in
            self.animatedGradient()
        }.store(in: &subscribers)
}

我可能错了,但记录的错误看起来像是苹果的问题。至于在应用程序重新激活时恢复动画,您可以尝试下面我在测试中使用的代码,这很有效

import Combine

...

class GradientBackground : UIViewController {

...
private var subscribers = [AnyCancellable]()

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

    NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification)
        .sink { _ in
            self.gradient.removeAllAnimations()
        }.store(in: &subscribers)
    NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
        .sink { _ in
            self.animatedGradient()
        }.store(in: &subscribers)
}