SwiftUI Timer.publish 导致整个屏幕刷新
SwiftUI Timer.publish causing whole screen to refresh
GIF of Entire Screen Refreshing
我目前正在学习 combine 和 MVVM。我的问题是当我尝试使用 timer.publish 时,最终我要创建一个停止按钮,它会导致整个屏幕刷新,而不是刷新我拥有的 .onReceive 文本。
我希望有人可以提供一些见解,让我了解我如何错误地使用发布者和观察者。
查看:
import SwiftUI
import Combine
struct ApptCardView: View {
@ObservedObject var apptCardVM: ApptCardViewModel
@State var currentDate = Date()
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
VStack {
Text("\(currentDate)")
.onReceive(timer) { input in
self.currentDate = input
}
Picker("Seizure Type", selection: $apptCardVM.typeIndex) {
ForEach(0..<apptCardVM.typeChoice.count) {
Text(self.apptCardVM.typeChoice[[=11=]])
}
}.pickerStyle(SegmentedPickerStyle())
}
}
}
查看模型:
import Foundation
import Combine
class ApptCardViewModel: ObservableObject, Identifiable {
@Published var appt: ApptEvent
@Published var typeChoice = ["Quick", "Long", "FullService"]
@Published var typeIndex: Int = 0
private var cancellables = Set<AnyCancellable>()
init(appt: ApptEvent) {
self.appt = appt
}
}
如果你只想刷新body的一部分,那么将那部分分开到专用的子视图中,例如:
struct ApptCardView: View {
@ObservedObject var apptCardVM: ApptCardViewModel
var body: some View {
VStack {
CurrentDateView() // << here !!
Picker("Seizure Type", selection: $apptCardVM.typeIndex) {
ForEach(0..<apptCardVM.typeChoice.count) {
Text(self.apptCardVM.typeChoice[[=10=]])
}
}.pickerStyle(SegmentedPickerStyle())
}
}
}
struct CurrentDateView: View {
@State private var currentDate = Date()
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
Text("\(currentDate)")
.onReceive(timer) { input in
self.currentDate = input // << refresh only own body !!
}
}
}
GIF of Entire Screen Refreshing
我目前正在学习 combine 和 MVVM。我的问题是当我尝试使用 timer.publish 时,最终我要创建一个停止按钮,它会导致整个屏幕刷新,而不是刷新我拥有的 .onReceive 文本。
我希望有人可以提供一些见解,让我了解我如何错误地使用发布者和观察者。
查看:
import SwiftUI
import Combine
struct ApptCardView: View {
@ObservedObject var apptCardVM: ApptCardViewModel
@State var currentDate = Date()
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
VStack {
Text("\(currentDate)")
.onReceive(timer) { input in
self.currentDate = input
}
Picker("Seizure Type", selection: $apptCardVM.typeIndex) {
ForEach(0..<apptCardVM.typeChoice.count) {
Text(self.apptCardVM.typeChoice[[=11=]])
}
}.pickerStyle(SegmentedPickerStyle())
}
}
}
查看模型:
import Foundation
import Combine
class ApptCardViewModel: ObservableObject, Identifiable {
@Published var appt: ApptEvent
@Published var typeChoice = ["Quick", "Long", "FullService"]
@Published var typeIndex: Int = 0
private var cancellables = Set<AnyCancellable>()
init(appt: ApptEvent) {
self.appt = appt
}
}
如果你只想刷新body的一部分,那么将那部分分开到专用的子视图中,例如:
struct ApptCardView: View {
@ObservedObject var apptCardVM: ApptCardViewModel
var body: some View {
VStack {
CurrentDateView() // << here !!
Picker("Seizure Type", selection: $apptCardVM.typeIndex) {
ForEach(0..<apptCardVM.typeChoice.count) {
Text(self.apptCardVM.typeChoice[[=10=]])
}
}.pickerStyle(SegmentedPickerStyle())
}
}
}
struct CurrentDateView: View {
@State private var currentDate = Date()
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
Text("\(currentDate)")
.onReceive(timer) { input in
self.currentDate = input // << refresh only own body !!
}
}
}