当前位置: 首页 > news >正文

LaunchView/启动页 的实现

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/// 是否显示启动 View@Binding var showLaunchView: Boolvar 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 inText(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 - 1if 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 {/// 主 ViewModel@StateObject private var viewModel = HomeViewModel()/// 是否显示启动 View@State private var showLaunchView: Bool = trueinit() {// 修改导航栏中标题的颜色, 适配 iOS 15 导航栏背景自动更改为默认颜色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)  // 设置导航栏背景色// let buttonAppearance = UIBarButtonItemAppearance()// buttonAppearance.normal.titleTextAttributes = [//    .foregroundColor: UIColor(Color.theme.accent)// ]//appBarAppearance.buttonAppearance = buttonAppearanceUINavigationBar.appearance().standardAppearance = barAppearance // 带scroll滑动的页面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) //前景色,按钮颜色//UINavigationBar.appearance().barTintColor = UIColor(Color.theme.background) //背景色,导航条背景色// 更改表格背景颜色UITableView.appearance().backgroundColor = .clear}}var body: some Scene {WindowGroup {ZStack {NavigationView {HomeView()//.navigationBarHidden(true)}// 适配 iPad 导航栏.navigationViewStyle(.stack)// 环境对象中添加 view model,便于每个 View 都能够去访问.environmentObject(viewModel).accentColor(Color.theme.accent)// 防止 Z 堆栈跳转时产生混乱问题ZStack {// 是否显示启动 Viewif showLaunchView {LaunchView(showLaunchView: $showLaunchView)//.transition(.move(edge: .leading))// transition: 过渡动画 .scale(scale: 0).transition(.move(edge: .leading))}}.zIndex(2.0)}}}
}

5. 效果图:

http://www.lryc.cn/news/194580.html

相关文章:

  • windows安装npm教程
  • 网络端口验证
  • MongoDB 索引和常用命令
  • 【超详细】win10安装docker
  • JVM调优(一)
  • Parallels Desktop 19中文-- PD19最新安装
  • 【c++】向webrtc学比较1:AheadOf、IsNewerTimestamp
  • 华为云云耀云服务器L实例评测|企业项目最佳实践之docker部署及应用(七)
  • MAC上使用Wireshark常见问题
  • 在C++中++a和a++有什么区别?
  • NewStarCTF2023公开赛道-压缩包们
  • oracle数据库增加表空间数据文件
  • 【08】基础知识:React中收集表单数据(非受控组件和受控组件)
  • 数据结构之堆排序和前,中,后,层序遍历,链式二叉树
  • 多线程中ThreadPoolExecutor.map()中传递多个参数
  • linux centos7 环境下 no such file or directory
  • Nginx 反向代理 SSL 证书绑定域名
  • SpringBoot 集成 JMS 与 IBMMQ 代码示例教程
  • 大模型之Prompt研究和技巧
  • 掌握Golang匿名函数
  • HarmonyOS云开发基础认证---练习题二
  • ffmpeg视频解码器的配置选项含义
  • enter ubuntu boot option in virt-manager
  • 电商运营该如何做 AB 测试
  • go环境部署
  • HTTP/2 中的漏洞
  • 智能油烟机 优化烹饪体验
  • 啥?PS一秒成图?Adobe的逆天黑科技大公开
  • 扫描器(xray和bp联动)
  • [C++]:1.初识C++和C语言缺陷补充。