启动画面

这是一个小巧的软件包,允许您创建一个自动淡入淡出的启动画面。基本上,您希望它首先具有与您的启动画面文件相同的静态内容。当启动画面消失后,将显示此启动画面,然后您可以对它的内容进行任何您想要的更改。

创建一个包含您的启动画面内容的 SplashContents 结构体。

//This is where you can get creative about what is displayed.
struct SplashContents: View {
    @Binding var splashShown: Bool

    var body: some View {
        ZStack {
            Color(.systemBackground)
                .ignoresSafeArea()
            LogoView()
                .onAppear {
                    splashShown = true
                }
        }
    }
}

在您的 Content 文件中创建一个布尔类型的状态变量。添加一个 ZStack,并在其中调用 SplashScreen,传递持续时间和延迟。在闭包中,传入您的 SplashContents,并将其与状态变量进行绑定。

@State private var splashShown = false

var body: some View {
    ZStack {
        MyAppView()
        SplashScreen(duration: 2.0, delay: 2.0) {
            SplashContents(splashShown: $splashShown)
        }
    }
}

就这么简单!