SwiftUI 应用程序在第二次连续打开后崩溃

SwiftUI App crashes after second continuous open

我有一个正在运行的 SwiftUI 应用程序,它的启动动画似乎有时会导致它崩溃 - 只有当用户在动画到达终点之前完全退出应用程序并立即重新打开应用程序时才会发生这种情况 - 它会跳过启动屏幕,直接进入主屏幕,然后崩溃。我已将我的代码放在下面 - 感谢您的帮助。


import SwiftUI
import PDFKit

struct ContentView: View {
    @Environment(\.presentationMode) var presentationMode
    @Environment(\.scenePhase) var scenePhase
    @State var inside = false
    @AppStorage("ShowLoadingScreen") var showLoadingScreen = UserDefaults.standard.value(forKey: "ShowingLoadingScreen") as? Bool ?? true
    var body: some View {
        NavigationView {
            ZStack {
                Rectangle()
                     .ignoresSafeArea()
                     .foregroundColor(Color("LightBlue"))
                     .navigationTitle("Home")
                     .navigationBarHidden(true)
                VStack {
                    if inside || !showLoadingScreen{
                    Spacer()
                      
                        Text("Bugle") .foregroundColor(Color("DarkBlue"))
                        .font(Font.custom("Copperplate", size: UIScreen.main.bounds.height * 0.1))  .padding(.top, UIScreen.main.bounds.height * 0.015)
                        .multilineTextAlignment(.center)
                        .padding()
                        Spacer()
                        .navigationBarHidden(true)
                   
                           
                    NavigationLink {
                        MonthlyBugleView()
                            .navigationTitle("This Month's Bugle")
                    } label: {
                        ZStack {
                            RoundedRectangle(cornerRadius: 100)
                                .frame(width: UIScreen.main.bounds.width * 0.9, height: UIScreen.main.bounds.height * 0.15)
                            .opacity(0.8)
                            Text("This Month's Bugle").font(Font.custom("Copperplate", size: UIScreen.main.bounds.height * 0.05))  .padding(.top, UIScreen.main.bounds.height * 0.015)
                                .foregroundColor(.white)
                                .padding()
                        }
                    }
                    Spacer()
                    NavigationLink {
                        PDFSwiftUIView(StringToBeLoaded: "SampleLink")
                    } label: {
                        ZStack {
                            RoundedRectangle(cornerRadius: 100)
                                .frame(width: UIScreen.main.bounds.width * 0.9, height: UIScreen.main.bounds.height * 0.15)
                            .opacity(0.8)
                            .foregroundColor(Color("DarkBlue"))
                            Text("Previous Bugles").font(Font.custom("Copperplate", size: UIScreen.main.bounds.height * 0.05))  .padding(.top, UIScreen.main.bounds.height * 0.015)
                                .foregroundColor(.white)
                                .padding()
                        }
                    }

         
                    Spacer()
                    
             NavigationLink {
                 MoreWinaduStuffView()
                     .navigationBarHidden(true)
             } label: {
                 ZStack {
                     RoundedRectangle(cornerRadius: 100)
                         .frame(width: UIScreen.main.bounds.width * 0.9, height: UIScreen.main.bounds.height * 0.15)
                     .opacity(0.8)
                     Text("More Stuff").font(Font.custom("Copperplate", size: UIScreen.main.bounds.height * 0.05))  .padding(.top, UIScreen.main.bounds.height * 0.015)
                         .foregroundColor(.white)
                 }
               
             }
                    Spacer()
                    

                    } else if showLoadingScreen{
                        StartAnimationView()
                           .transition(.opacity)
                    }
                  
                }
             
                
                
                .onAppear( perform: {
                    DispatchQueue.main.asyncAfter(deadline: .now()+3.5){
                        withAnimation {
                            inside.toggle()
                            showLoadingScreen = false
                        }
                       
                 
                    
                }
                }
            )
                
            }
        } .onChange(of: scenePhase) { newPhase in
            if newPhase == .inactive {
               showLoadingScreen = true
            } else if newPhase == .active {
                if inside == true{
                    showLoadingScreen = false
                }
            } else if newPhase == .background {
                showLoadingScreen = true
            }
        }
        
        
    }
   
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

这是我的 StartAnimationView:


import SwiftUI

struct StartAnimationView: View {
    let color: UIColor = UIColor(red: 29/255.0, green: 161/255.0, blue: 242/255.0, alpha: 1)
    @State var animate: Bool = false
    @State var offset: CGFloat = 0
    var body: some View {
        VStack{
            //Content
            
            ZStack{
              
                Color("LightBlue")
                   
                Image("sample start")
                    .resizable()
                    .scaledToFit()
                    .padding()
                    .offset(x: 0, y: offset)
                    .foregroundColor(.white)
                    .frame(width: 150, height: 150, alignment: .center)
                    .scaleEffect(animate ? UIScreen.main.bounds.height * 0.2 : UIScreen.main.bounds.height * 0.0075)
                    .animation(.easeIn(duration: 3.5), value: animate)
                    .animation(.easeIn(duration: 0.1), value: offset)
                   
                    
                
            } .ignoresSafeArea()
        }.onAppear{
            DispatchQueue.main.asyncAfter(deadline: .now()+0.3){
                offset = 8
                animate.toggle()
             
                
            }
        }
    }
}


struct StartAnimationView_Previews: PreviewProvider {
    static var previews: some View {
        StartAnimationView()
    }
}

您在 newPhase 的 onChange 中的操作可能会使 SwiftUI 感到困惑,因为您要求 SwiftUI 在进入非活动状态或后台时显示动画。