Oops I seem to have deleted more than my commented text in the first post. This would be the complete code.
struct ContentView: View {
@State var date = Date()
var body: some View {
ZStack {
Color.black
.edgesIgnoringSafeArea(.all)
Text("00:00:00").opacity(0.3)
.foregroundColor(.accentColor)
.font(.system(size: 50, weight: .ultraLight, design: .default))
Text("\(timeString(date: date))")
.foregroundColor(.white)
.onAppear(perform: {let _ = self.updateTimer})
.font(.system(size: 45, weight: .ultraLight, design: .default))
}
}
var timeFormat: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm:ss"
return formatter
}
func timeString(date: Date) -> String {
let time = timeFormat.string(from: date)
return time
}
var updateTimer: Timer {
Timer.scheduledTimer(withTimeInterval: 1, repeats: true,
block: {_ in
self.date = Date()
})
}
}
Post
Replies
Boosts
Views
Activity
Yes, that worked exactly as I wanted. Thanks!