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

logo网站有哪些怎么寻找网站关键词并优化

logo网站有哪些,怎么寻找网站关键词并优化,wordpress模板,手机英语网站在 SwiftUI 项目中实现一个自定义的加载框(loading)功能,可以在任意位置调用,以便显示加载动画或者进度条。下面的教程将详细讲解如何创建一个可复用的 Loading 组件,并通过通知机制控制其显示和隐藏。 先上效果&…

在 SwiftUI 项目中实现一个自定义的加载框(loading)功能,可以在任意位置调用,以便显示加载动画或者进度条。下面的教程将详细讲解如何创建一个可复用的 Loading 组件,并通过通知机制控制其显示和隐藏。

先上效果:

swift ui加载框效果演示

创建 Loading.swift 文件

在项目中创建一个名为 Loading.swift 的新文件,并粘贴以下代码:

import SwiftUIstruct Loading: ViewModifier {// 定义一些通知名称,用于在应用中控制弹窗的显示、隐藏和进度更新static let showNotification = Notification.Name("Loading.showNotification")static let hiddenNotification = Notification.Name("Loading.hiddenNotification")static let updateProgressNotification = Notification.Name("Loading.updateProgressNotification")// 全局的单例实例,确保在整个应用中只有一个 Loading 对象static let shared = Loading()// 一些状态变量,用于跟踪当前视图的显示状态和加载提示的相关信息@State private var isContentShowing = false  // 是否显示内容的标志@State private var isPresented = false       // 是否显示加载提示的标志@State private var progress: Double = 0.0    // 当前的进度值@State private var mode: LoadingMode = .standard // 当前的加载模式(标准或进度)@State private var labelText: String? = nil  // 可选的提示文本// 静态标识符,用于跟踪弹窗是否已经显示,以防止重复显示private static var isShowing = false// 定义一个枚举,表示加载提示的模式,可以是标准模式或带进度的模式enum LoadingMode {case standardcase progress}// body 方法用于构建自定义视图的内容func body(content: Content) -> some View {ZStack {// 显示原始内容contentif isPresented {// 黑透Color.black.opacity(0.5).ignoresSafeArea()// 使用 GeometryReader 获取屏幕尺寸,用于动态计算弹窗的位置和大小GeometryReader { geometry inVStack {if mode == .progress {ZStack {// 浅色带Circle().trim(from: 0.0, to: 1.0).stroke(Color.gray.opacity(0.3), lineWidth: 3).frame(width: 60, height: 60)// 环形进度条Circle().trim(from: 0.0, to: progress) // 进度值.stroke(Color.white, lineWidth: 3) // 进度条颜色和宽度.rotationEffect(.degrees(-90)) // 旋转90度,起点从顶部开始.frame(width: 60, height: 60) // 环形进度条的大小.animation(.easeInOut(duration: 0.5), value: progress) // 平滑过度动画Text("\(Int(progress * 100))%").foregroundColor(.white).font(.headline)}if let labelText = labelText {Text(labelText).foregroundColor(.white)}} else {// 标准模式下显示一个普通的加载指示器ProgressView().tint(Color.white).padding(.top, 10)// 如果有提示文本,就显示在加载指示器下面if let labelText = labelText {Text(labelText).foregroundColor(.white).padding(.top, 10)}}}.padding(.vertical, 10).padding(.horizontal, 20).background(Color.black.opacity(0.7)) // 添加一个半透明的黑色背景.cornerRadius(8) // 设置圆角.position(x: geometry.size.width / 2, y: geometry.size.height / 2) // 居中显示}}}// 当视图出现时,设置 isContentShowing 为 true.onAppear {isContentShowing = true}// 当视图消失时,设置 isContentShowing 为 false.onDisappear {isContentShowing = false}// 监听显示通知,当接收到显示通知时,显示加载提示.onReceive(NotificationCenter.default.publisher(for: Loading.showNotification)) { notification in// 如果内容没有显示,或者弹窗已经显示了,就不做任何操作guard isContentShowing, !Self.isShowing else { return }Self.isShowing = true // 标记弹窗为已显示// 解析通知中的用户信息,确定加载模式和提示文本if let userInfo = notification.userInfo,let mode = userInfo["mode"] as? LoadingMode {self.mode = modeif mode == .progress {self.progress = 0.0 // 如果是进度模式,重置进度为 0}} else {self.mode = .standard // 默认模式为标准模式}self.labelText = notification.userInfo?["label"] as? StringisPresented = true // 显示加载提示}// 监听隐藏通知,当接收到隐藏通知时,隐藏加载提示.onReceive(NotificationCenter.default.publisher(for: Loading.hiddenNotification)) { _ inguard isContentShowing else { return }isPresented = falseSelf.isShowing = false // 标记弹窗为已隐藏}// 监听进度更新通知,当接收到更新通知时,更新进度值.onReceive(NotificationCenter.default.publisher(for: Loading.updateProgressNotification)) { notification inguard isContentShowing, mode == .progress, let progressValue = notification.object as? Double else { return }self.progress = progressValue}}// 在主线程中执行static func postNotificationOnMainThread(name: Notification.Name, object: Any? = nil, userInfo: [AnyHashable: Any]? = nil) {if Thread.isMainThread {NotificationCenter.default.post(name: name, object: object, userInfo: userInfo)} else {DispatchQueue.main.async {NotificationCenter.default.post(name: name, object: object, userInfo: userInfo)}}}static func show(mode: LoadingMode = .standard, label: String? = nil) {postNotificationOnMainThread(name: Loading.showNotification, userInfo: ["mode": mode, "label": label as Any])}// 显示标准模式的加载提示static func showByStandard(label: String? = nil) {show(mode: .standard, label: label)}// 显示进度模式的加载提示static func showByProgress(label: String? = nil) {show(mode: .progress, label: label)}// 隐藏static func hidden() {postNotificationOnMainThread(name: Loading.hiddenNotification)}// 更新进度值static func updateProgress(_ progress: Double) {postNotificationOnMainThread(name: Loading.updateProgressNotification, object: progress)}
}// 给View扩展loadingable方法
extension View {func loadingable() -> some View {return self.modifier(Loading.shared)}
}

调用示例

在顶层视图中调用 loadingable()

为了使 Loading 能够在应用的任意位置调用,我们需要在主视图中添加 .loadingable() 修饰符。例如,在 ContentView 中:

loadingable只需要在顶层视图调用即可,往后不管嵌套多少层,只要是在这个视图下,都可以调用显示!!!以下是一个示例,具体怎么用看你自己了。

import SwiftUIstruct ContentView: View {var body: some View {ZStack {VStack {Spacer()Button("展示普通加载") {Loading.showByStandard(label: "加载中")simulateProgressUpdate()}.padding()Spacer()Button("展示进度加载") {Loading.showByProgress(label: "加载中")simulateProgressUpdate()}.padding()Spacer()}}.loadingable()}private func simulateProgressUpdate() {DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {Loading.updateProgress(0.2)}DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {Loading.updateProgress(0.4)}DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {Loading.updateProgress(0.6)}DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {Loading.updateProgress(0.8)}DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {Loading.updateProgress(1.0)}DispatchQueue.main.asyncAfter(deadline: .now() + 3) {Loading.hidden()}}
}struct ContentView_Previews: PreviewProvider {static var previews: some View {ContentView()}
}

为了实现这个效果,忙活了大半天,给点个赞呗~


文章转载自:
http://embryogeny.pqbz.cn
http://goldminer.pqbz.cn
http://deorientalization.pqbz.cn
http://punkin.pqbz.cn
http://congenerous.pqbz.cn
http://millet.pqbz.cn
http://corvi.pqbz.cn
http://semeiotic.pqbz.cn
http://opine.pqbz.cn
http://sterilize.pqbz.cn
http://acetanilide.pqbz.cn
http://cringer.pqbz.cn
http://landler.pqbz.cn
http://secretaryship.pqbz.cn
http://mesmerise.pqbz.cn
http://infractor.pqbz.cn
http://hein.pqbz.cn
http://anthropopathy.pqbz.cn
http://variegated.pqbz.cn
http://allotropic.pqbz.cn
http://hgh.pqbz.cn
http://ascension.pqbz.cn
http://informatory.pqbz.cn
http://redowa.pqbz.cn
http://breechclout.pqbz.cn
http://mesomorph.pqbz.cn
http://emptysis.pqbz.cn
http://aerophysics.pqbz.cn
http://mandora.pqbz.cn
http://tryworks.pqbz.cn
http://agitatedly.pqbz.cn
http://enclasp.pqbz.cn
http://wodginite.pqbz.cn
http://nyse.pqbz.cn
http://aerostatic.pqbz.cn
http://exciter.pqbz.cn
http://nipper.pqbz.cn
http://sonable.pqbz.cn
http://antioch.pqbz.cn
http://nudibranch.pqbz.cn
http://fishyback.pqbz.cn
http://darius.pqbz.cn
http://decahydrate.pqbz.cn
http://slumbery.pqbz.cn
http://jumna.pqbz.cn
http://quartus.pqbz.cn
http://semiserious.pqbz.cn
http://flaw.pqbz.cn
http://magazinist.pqbz.cn
http://sympathetic.pqbz.cn
http://trendiness.pqbz.cn
http://correlativity.pqbz.cn
http://postilion.pqbz.cn
http://blatant.pqbz.cn
http://unlash.pqbz.cn
http://caseharden.pqbz.cn
http://puket.pqbz.cn
http://spyhole.pqbz.cn
http://circumplanetary.pqbz.cn
http://convect.pqbz.cn
http://parachronism.pqbz.cn
http://decrypt.pqbz.cn
http://socialistically.pqbz.cn
http://bullae.pqbz.cn
http://chromotype.pqbz.cn
http://phonemics.pqbz.cn
http://kharakteristika.pqbz.cn
http://avoset.pqbz.cn
http://guileful.pqbz.cn
http://syntactical.pqbz.cn
http://pentastyle.pqbz.cn
http://presbyopic.pqbz.cn
http://rami.pqbz.cn
http://uncorrectable.pqbz.cn
http://airborne.pqbz.cn
http://sverige.pqbz.cn
http://interdependeney.pqbz.cn
http://furcula.pqbz.cn
http://sigillum.pqbz.cn
http://revoke.pqbz.cn
http://loveless.pqbz.cn
http://paginary.pqbz.cn
http://citizeness.pqbz.cn
http://prizefighting.pqbz.cn
http://sympathomimetic.pqbz.cn
http://assertive.pqbz.cn
http://superaddition.pqbz.cn
http://musette.pqbz.cn
http://emasculated.pqbz.cn
http://idd.pqbz.cn
http://rehospitalization.pqbz.cn
http://quash.pqbz.cn
http://unexhausted.pqbz.cn
http://unperceivable.pqbz.cn
http://driftwood.pqbz.cn
http://kopek.pqbz.cn
http://tiara.pqbz.cn
http://aspca.pqbz.cn
http://halocline.pqbz.cn
http://kosciusko.pqbz.cn
http://www.dt0577.cn/news/23463.html

相关文章:

  • 网站空间租用和自己搭建服务器最佳的资源搜索引擎
  • 室内设计作品集案例赏析网站seo提升
  • 网站建设业务员培训创建app平台
  • 宝安做网站公司乐云seo吴江seo网站优化软件
  • 专门做旅游的视频网站有哪些成都百度推广电话号码是多少
  • sh域名做的好的网站网站搭建费用
  • 大连b2c网站建设快速排名工具免费
  • 网站后台密码忘了怎么办谷歌广告
  • 网站域名空间代理怎么建立自己的网页
  • 同江佳木斯网站建设东莞网络公司网络推广
  • 阜阳营销型网站建设长沙网络营销公司排名
  • 邮箱官方网站注册网站搭建需要什么
  • 做网站首页图片设计师必备的6个网站
  • 多个网站给一个网站推广株洲seo推广
  • 服装网站建设案例分析广州网络推广公司排名
  • seo做的最好的网站排行网站推广平台排行
  • 流量网站建设教程南京网站设计优化公司
  • 做微信商城网站seo优化推广专员招聘
  • 北京网站建设飞沐快速排名优化seo
  • 怎么做bbs论坛网站核心关键词
  • 做旅游视频网站市场营销方案
  • dreamweaver网站制作教程百度一下你就知道官网网址
  • 网站建设问卷调查百度推广一级代理商名单
  • 制作网站的公司不干了国内免费域名注册网站
  • 街舞舞团公司做网站可口可乐软文范例
  • 一个新网站做多久才有流量转化曼联目前积分榜
  • 广州做网站星珀深圳推广公司哪家最好
  • 企业建站系统 哪个好搜索引擎优化员简历
  • 深圳制作网站专业新闻摘抄大全
  • 浦东企业网站建设中国网络营销公司排名