• LaunchView/启动页 的实现


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

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

    3. 创建启动画面动画视图,LaunchView.swift

    1. import SwiftUI
    2. /// 启动视图
    3. struct LaunchView: View {
    4. /// 字符串转换为字符串数组,字符串中包含单个字母组成
    5. @State private var loadingText: [String] = "Loading your portfolio...".map { String($0) }
    6. /// 是否显示文字
    7. @State private var showLoadingText: Bool = false
    8. /// 计时器
    9. private let timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
    10. /// 计数
    11. @State private var counter: Int = 0
    12. /// 循环次数
    13. @State private var loops: Int = 0
    14. /// 是否显示启动 View
    15. @Binding var showLaunchView: Bool
    16. var body: some View {
    17. ZStack {
    18. // 背景颜色
    19. Color.launch.background.ignoresSafeArea()
    20. // 图标
    21. Image("logo-transparent")
    22. .resizable()
    23. .frame(width: 100, height: 100)
    24. // 文字
    25. ZStack {
    26. if showLoadingText {
    27. HStack(spacing: 0) {
    28. ForEach(loadingText.indices, id: \.self) { index in
    29. Text(loadingText[index])
    30. .font(.headline)
    31. .fontWeight(.heavy)
    32. .foregroundColor(Color.launch.accent)
    33. .offset(y: counter == index ? -5 : 0)
    34. }
    35. }
    36. .transition(AnyTransition.scale.animation(.easeIn))
    37. }
    38. }
    39. .offset(y: 70)
    40. }
    41. .onAppear {
    42. showLoadingText.toggle()
    43. }
    44. .onReceive(timer) { _ in
    45. // 添加弹簧动画
    46. withAnimation(.spring()) {
    47. let lastIndex = loadingText.count - 1
    48. if counter == lastIndex {
    49. counter = 0
    50. // 循环多少次
    51. loops += 1
    52. // 检查次数
    53. if loops >= 2 {
    54. showLaunchView = false
    55. }
    56. }else{
    57. counter += 1
    58. }
    59. }
    60. }
    61. }
    62. }
    63. struct LaunchView_Previews: PreviewProvider {
    64. static var previews: some View {
    65. LaunchView(showLaunchView: .constant(true))
    66. }
    67. }

    4. 启动结构体中添加版本适配、启动页、主页,SwiftfulCryptoApp.swift

    1. import SwiftUI
    2. @main
    3. struct SwiftfulCryptoApp: App {
    4. /// 主 ViewModel
    5. @StateObject private var viewModel = HomeViewModel()
    6. /// 是否显示启动 View
    7. @State private var showLaunchView: Bool = true
    8. init() {
    9. // 修改导航栏中标题的颜色, 适配 iOS 15 导航栏背景自动更改为默认颜色
    10. if #available(iOS 15, *) {
    11. let barAppearance = UINavigationBarAppearance()
    12. barAppearance.configureWithOpaqueBackground() // 重置背景和阴影颜色
    13. barAppearance.titleTextAttributes = [.foregroundColor: UIColor(Color.theme.accent) ]
    14. barAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor(Color.theme.accent)]
    15. barAppearance.backgroundColor = UIColor(Color.theme.background) // 设置导航栏背景色
    16. // let buttonAppearance = UIBarButtonItemAppearance()
    17. // buttonAppearance.normal.titleTextAttributes = [
    18. // .foregroundColor: UIColor(Color.theme.accent)
    19. // ]
    20. //appBarAppearance.buttonAppearance = buttonAppearance
    21. UINavigationBar.appearance().standardAppearance = barAppearance // 带scroll滑动的页面
    22. UINavigationBar.appearance().scrollEdgeAppearance = barAppearance // 常规页面
    23. UINavigationBar.appearance().compactAppearance = barAppearance
    24. }else{
    25. UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor(Color.theme.accent)]
    26. UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor(Color.theme.accent)]
    27. UINavigationBar.appearance().backgroundColor = UIColor(Color.theme.background)
    28. UINavigationBar.appearance().tintColor = UIColor(Color.theme.accent) //前景色,按钮颜色
    29. //UINavigationBar.appearance().barTintColor = UIColor(Color.theme.background) //背景色,导航条背景色
    30. // 更改表格背景颜色
    31. UITableView.appearance().backgroundColor = .clear
    32. }
    33. }
    34. var body: some Scene {
    35. WindowGroup {
    36. ZStack {
    37. NavigationView {
    38. HomeView()
    39. //.navigationBarHidden(true)
    40. }
    41. // 适配 iPad 导航栏
    42. .navigationViewStyle(.stack)
    43. // 环境对象中添加 view model,便于每个 View 都能够去访问
    44. .environmentObject(viewModel)
    45. .accentColor(Color.theme.accent)
    46. // 防止 Z 堆栈跳转时产生混乱问题
    47. ZStack {
    48. // 是否显示启动 View
    49. if showLaunchView {
    50. LaunchView(showLaunchView: $showLaunchView)
    51. //.transition(.move(edge: .leading))
    52. // transition: 过渡动画 .scale(scale: 0)
    53. .transition(.move(edge: .leading))
    54. }
    55. }
    56. .zIndex(2.0)
    57. }
    58. }
    59. }
    60. }

    5. 效果图:

  • 相关阅读:
    开篇-开启全新的.NET现代应用开发体验
    编译CentOS6.10系统的OpenSSHV9.4rpm安装包
    Python os模块
    Flir Blackfly S USB3 工业相机:计数器和定时器的使用方法
    2022年中职组网络安全新赛题
    基于Springboot+Vue开发建筑工地用料管理系统
    我的十年程序员生涯--考研失利,倒也还好
    内存中为什么分堆和栈,能否只用一种模型呢?为什么每个线程都有单独的栈
    八、【React拓展】错误边界
    App测试需要测什么
  • 原文地址:https://blog.csdn.net/u011193452/article/details/133769701