如何在 Swift 5 中使用 TimelineView 每秒刷新一个文本?
我对快速编码很陌生,我正在尝试做一个非常简单的项目:只是一个显示时间的时钟。我正在使用 TimelineView 每秒刷新时间,但它不起作用。这是我的代码:
import SwiftUI
struct ContentView: View {
@State var hour: Int = Calendar.current.component(.hour, from: Date())
@State var minute: Int = Calendar.current.component(.minute, from: Date())
@State var second: Int = Calendar.current.component(.second, from: Date())
var body: some View {
ZStack {
VStack{
Spacer()
HStack {
TimelineView(.periodic(from: .now, by: 1)) { timeline in
Text(String(hour))
Text(String(minute))
Text(String(second))
}
}
Spacer()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView()
.previewInterfaceOrientation(.portrait)
}
}
}
由于我的小时、分钟和秒变量是 @State 并且我正在使用 TimelineView,它们应该每秒刷新一次,不是吗?
我很困惑,希望得到一些帮助。非常感谢。
I'm pretty new at swift coding and I'm trying to do a very simple project: just a clock that shows the time. I'm using TimelineView to refresh the time every second, but it's not working. This is my code:
import SwiftUI
struct ContentView: View {
@State var hour: Int = Calendar.current.component(.hour, from: Date())
@State var minute: Int = Calendar.current.component(.minute, from: Date())
@State var second: Int = Calendar.current.component(.second, from: Date())
var body: some View {
ZStack {
VStack{
Spacer()
HStack {
TimelineView(.periodic(from: .now, by: 1)) { timeline in
Text(String(hour))
Text(String(minute))
Text(String(second))
}
}
Spacer()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView()
.previewInterfaceOrientation(.portrait)
}
}
}
Since my hour, minute and second variables are @State and I'm using the TimelineView, they should refresh every second, shouldn't they?
I'm very confused and I would appreciate some help. Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如文档所述(https://developer.apple.com/documentation/swiftui/timelineview):
它包含的内容在您提供的闭包中定义:
在这个闭包中,您可以访问
TimelineView.Context
(https://developer.apple.com/documentation/swiftui/timelineview/context)。借助此上下文,您可以访问触发更新/重画的日期,如下所示:这将产生以下输出:
要改进格式设置,您可以使用
DateFormatter
(https://developer.apple.com/documentation/foundation/dateformatter):As the documentation says (https://developer.apple.com/documentation/swiftui/timelineview):
The content it contains is defined in the closure you provide:
Inside this closure you have access to a
TimelineView.Context
(https://developer.apple.com/documentation/swiftui/timelineview/context). With the help of this context, you can access the date which triggered the update / redraw like so:This will produce the following output:
To improve formatting, you could use a
DateFormatter
(https://developer.apple.com/documentation/foundation/dateformatter):你必须观察时间线的变化。
这里我使用了
onChange
并更新了分钟、秒和小时的值。You have to observe changes in the timeline.
Here I used
onChange
and update the value of min, sec, and hour.只需将小时-分钟-秒作为
计算属性
而不是@State
Just make your hour-minute-second as
computed property
not@State