Text(.now, format:.timer(countingDownIn: Date.now..<Date.now.addingTimeInterval(120),
showsHours: true,
maxFieldCount: 2,
maxPrecision: .seconds(60))
)
Using Date.now here confused me 🤔. The first argument .now will be evaluated when the view body is updated, in a very unpredictable way. The text will stay on the last view update time.
I think this new API should be used in this way:
@State private var date: Date = .now
var body: some View {
Text(date, format: .timer(...))
.onReceive(Timer.publish(/* on every seconds */)) {
date = $0
}
}
UPDATE:
Found: https://developer.apple.com/documentation/swiftui/timedatasource/currentdate
Simply change Date.now to TimeDataSource<Date>.currentDate.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: