1. 创建启动画板,LaunchScreen.storyboard 添加组件如图:

2. 项目中设置只支持竖屏,添加启动画板,如图:

3. 创建启动画面动画视图,LaunchView.swift
import SwiftUI
struct LaunchView: View {
@State private var loadingText: [String] = "Loading your portfolio...".map { String($0) }
@State private var showLoadingText: Bool = false
private let timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
@State private var counter: Int = 0
@State private var loops: Int = 0
@Binding var showLaunchView: Bool
var body: some View {
ZStack {
Color.launch.background.ignoresSafeArea()
Image("logo-transparent")
.resizable()
.frame(width: 100, height: 100)
ZStack {
if showLoadingText {
HStack(spacing: 0) {
ForEach(loadingText.indices, id: \.self) { index in
Text(loadingText[index])
.font(.headline)
.fontWeight(.heavy)
.foregroundColor(Color.launch.accent)
.offset(y: counter == index ? -5 : 0)
}
}
.transition(AnyTransition.scale.animation(.easeIn))
}
}
.offset(y: 70)
}
.onAppear {
showLoadingText.toggle()
}
.onReceive(timer) { _ in
withAnimation(.spring()) {
let lastIndex = loadingText.count - 1
if counter == lastIndex {
counter = 0
loops += 1
if loops >= 2 {
showLaunchView = false
}
}else{
counter += 1
}
}
}
}
}
struct LaunchView_Previews: PreviewProvider {
static var previews: some View {
LaunchView(showLaunchView: .constant(true))
}
}
4. 启动结构体中添加版本适配、启动页、主页,SwiftfulCryptoApp.swift
import SwiftUI
@main
struct SwiftfulCryptoApp: App {
@StateObject private var viewModel = HomeViewModel()
@State private var showLaunchView: Bool = true
init() {
if #available(iOS 15, *) {
let barAppearance = UINavigationBarAppearance()
barAppearance.configureWithOpaqueBackground()
barAppearance.titleTextAttributes = [.foregroundColor: UIColor(Color.theme.accent) ]
barAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor(Color.theme.accent)]
barAppearance.backgroundColor = UIColor(Color.theme.background)
UINavigationBar.appearance().standardAppearance = barAppearance
UINavigationBar.appearance().scrollEdgeAppearance = barAppearance
UINavigationBar.appearance().compactAppearance = barAppearance
}else{
UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor(Color.theme.accent)]
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor(Color.theme.accent)]
UINavigationBar.appearance().backgroundColor = UIColor(Color.theme.background)
UINavigationBar.appearance().tintColor = UIColor(Color.theme.accent)
UITableView.appearance().backgroundColor = .clear
}
}
var body: some Scene {
WindowGroup {
ZStack {
NavigationView {
HomeView()
}
.navigationViewStyle(.stack)
.environmentObject(viewModel)
.accentColor(Color.theme.accent)
ZStack {
if showLaunchView {
LaunchView(showLaunchView: $showLaunchView)
.transition(.move(edge: .leading))
}
}
.zIndex(2.0)
}
}
}
}
5. 效果图:
