如何在 Swift UI 中每小时更改显示(例如星巴克咖啡应用程序)

发布于 2025-01-09 17:46:25 字数 184 浏览 1 评论 0原文

在此处输入图像描述

我希望 Swift UI 显示在像星巴克应用程序一样在指定时间导航查看(“早上好”在 5:00,“下午好”在 12:00,“晚安”在 17:00)。请告诉我。

enter image description here

I want the Swift UI to display in the Navigation View at specified times like the Starbucks app ("good morning" at 5:00, "good afternoon" at 12:00, "good night" at 17:00). please tell me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夢归不見 2025-01-16 17:46:25

您可以将当前时间与为每个标题设置的时间进行比较,以达到您想要的效果

import SwiftUI

struct SwiftUIView: View {
    // MARK: - PROPERTIES
    
    @State private var navigationTitle = ""
    
    // MARK: - BODY
    
    var body: some View {
        NavigationView {
            VStack{    
            }//: VSTACK
            .navigationTitle(navigationTitle) //TITLE
        }//: NAVIGATION
        .onAppear {
            let morningTime = "05:00:00"
            let noonTime = "12:00:00"
            let nightTime = "17:00:00"
            //DateFormatter
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "hh:mm:ss"
            let currentTime = dateFormatter.string(from: Date())
            //Conditions to compare time
            if currentTime >= morningTime && currentTime < noonTime{
                navigationTitle = "Good Morning"
            }else if currentTime >= noonTime && currentTime < nightTime{
                navigationTitle = "Good Afternoon"
            }else{
                navigationTitle = "Good Night"
            }
        }
    }
}

// MARK: - PREVIEW

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}

You can compare current time with the time you have set for each title to achieve what you're looking for

import SwiftUI

struct SwiftUIView: View {
    // MARK: - PROPERTIES
    
    @State private var navigationTitle = ""
    
    // MARK: - BODY
    
    var body: some View {
        NavigationView {
            VStack{    
            }//: VSTACK
            .navigationTitle(navigationTitle) //TITLE
        }//: NAVIGATION
        .onAppear {
            let morningTime = "05:00:00"
            let noonTime = "12:00:00"
            let nightTime = "17:00:00"
            //DateFormatter
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "hh:mm:ss"
            let currentTime = dateFormatter.string(from: Date())
            //Conditions to compare time
            if currentTime >= morningTime && currentTime < noonTime{
                navigationTitle = "Good Morning"
            }else if currentTime >= noonTime && currentTime < nightTime{
                navigationTitle = "Good Afternoon"
            }else{
                navigationTitle = "Good Night"
            }
        }
    }
}

// MARK: - PREVIEW

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文