要重现问题,请使用以下代码并执行以下操作步骤:
- 点击TestB View两次,
- 向下滚动关闭页面,过程中会出现上述问题。
import SwiftUI
@main
struct SwiftUIStudyApp: App {
@State private var isShow: Bool = false
var body: some Scene {
WindowGroup {
TestA().environmentObject(ObservableTest())
}
}
}
class ObservableTest: ObservableObject {
@Published var test: Bool = false
}
struct TestA: View {
@State var show: Bool = false
var body: some View {
VStack(alignment: .center, spacing: 0, content: {
Spacer()
Text("next")
.onTapGesture {
show.toggle()
}
Spacer()
})
.sheet(isPresented: $show, content: {
TestB()
})
}
}
struct TestB: View {
@EnvironmentObject var object: ObservableTest
var body: some View {
VStack(alignment: .center, spacing: 0, content: {
RoundedRectangle(cornerRadius: 25.0)
.fill(object.test == true ? Color.red : Color.green)
.onTapGesture {
object.test.toggle()
}
})
}
}