StepPreview 打印两次。当视图加载时(选择选项卡时)一次,当用户返回选项卡时再次。在 NavigationView 上使用 onAppear 时这是预期的吗?
struct StepPreview: View {
var body: some View {
NavigationView {
ScrollView {
LazyVStack{
ForEach(steps, id: \.id) { step in
VStack(spacing: 15){
Text("\(step.count)")
.font(.custom(customFont, size: 100))
.fontWeight(.semibold)
.multilineTextAlignment(.center)
.opacity(5)
.aspectRatio(contentMode: .fill)
.frame(width: 200, height: 200)
.padding(.bottom, -45)
}
}
}
.onAppear {
if let healthStore = healthStore {
healthStore.requestAuthorization { success in
if success {
healthStore.calculateSteps { statisticsCollection in
if let statisticsCollection = statisticsCollection {
// update the UI
updateUIFromStatistics(statisticsCollection)
}
}
}
}
}
}
}
}