如何像 Starbucks Coffee 应用程序一样在 Swift UI 每小时更改一次显示
How to change the display every hour in Swift UI like the Starbucks Coffee app
enter image description here
我希望 Swift UI 在指定时间显示在导航视图中,就像星巴克应用程序一样(“早上好”在 5:00,“下午好”在 12:00,17:00 的“晚安”)。请告诉我。
您可以将当前时间与您为每个标题设置的时间进行比较,以实现您要查找的内容
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()
}
}
enter image description here
我希望 Swift UI 在指定时间显示在导航视图中,就像星巴克应用程序一样(“早上好”在 5:00,“下午好”在 12:00,17:00 的“晚安”)。请告诉我。
您可以将当前时间与您为每个标题设置的时间进行比较,以实现您要查找的内容
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()
}
}