swift - 从 .onAppear 内部修改状态会导致错误“在视图更新期间修改状态,这将导致未定义的行为”
问题描述
在真实设备上进行测试时,我遇到了Modifying state during view update, this will cause undefined behavior
以下代码的问题。在 iPhone 11 上测试。在模拟器上,问题没有出现,代码按预期工作。
据我所知,从.onAppear 内部修改状态很好。
这里发生了什么?为什么我会收到此错误?
struct Clouds: View {
@State private var cloudOffset: CGFloat = 0.0
@State private var cloudOffset2: CGFloat = 0.0
@EnvironmentObject var mainData: MainData
var body: some View {
GeometryReader { geo in
if mainData.animateClouds {
Image("cloudsBlurry")
.resizable()
.scaledToFit()
.offset(x: self.cloudOffset, y: 0.0)
.onAppear {
let baseAnimation = Animation.linear(duration: 30)
let repeated = baseAnimation.repeatForever(autoreverses: true)
withAnimation(repeated) {
self.cloudOffset += geo.size.width * 0.50 //Error here
}
}
Image("cloudsBlurry3")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: geo.size.height * 2)
.offset(x: self.cloudOffset2, y: 0.0)
.onAppear {
let baseAnimation = Animation.linear(duration: 30)
let repeated = baseAnimation.repeatForever(autoreverses: true)
self.cloudOffset2 = -geo.size.width //Error here
withAnimation(repeated) {
self.cloudOffset2 += geo.size.width * 0.50 //Error here
}
}
} else {
Image("cloudsBlurry")
.resizable()
.scaledToFit()
.offset(x: 0.0, y: 0.0)
}
}
}
}
解决方案
您有循环,因为offset(x: self.cloudOffset
依赖并self.cloudOffset += geo
由于上述依赖而强制刷新。
尝试稍微延迟进行更新,以使当前刷新以当前偏移值完成:
withAnimation(repeated) {
DispatchQueue.main.async {
self.cloudOffset += geo.size.width * 0.50
}
}
推荐阅读
- javascript - 在 javascript 中将 SQL 数据库转换为 RDF
- xml - odoo 11如何继承和显示一些视野
- php - 如何从 mysql 数据库中选择随机行(比如 100 行中的 10 行)并用一系列数字显示它们
- angular - 通过第二个对话窗口以角度关闭第一个对话窗口
- html - 构建圆形百分比图表
- html - 如何将标签与 CSS 中 div 的“BOTTOM”对齐?
- react-native - 我如何在本机反应中设置选择器的样式
- iis - OPTIONS/200OK 之后的下一步
- pandas - 熊猫数据框添加新列并将现有列方向更改为索引
- php - 使用查询字符串重定向 URL?