im getting problem to make updates on my view by changing scroll progress.
As example let's take case when user start scrolling view, and after value >= 50, subview makes offset by Y to 10. Animation works, but im getting message on my console:
[SwiftUI] Publishing changes from within view updates is not allowed, this will cause undefined behavior.
struct TestViw: View {
@State var animate: Bool = false
var body: some View {
VStack(spacing: 0) {
Rectangle().fill(Color.red)
.offset(y: animate ? 10 : 0)
ScrollView{
VStack{
ForEach(0...100, id: \.self) {
Text(String($0))
}
}
.readProgress(onChange: { progress in
withAnimation{ animate = progress >= 50 ? true : false }
})
}.coordinateSpace(name: "scrollProgress")
}
}
}
Extension:
extension View {
func readProgress(onChange: @escaping (CGFloat) -> Void) -> some View {
background(
GeometryReader { geo in
let value = -geo.frame(in: .named("scrollProgress")).minY
Color.clear
.preference(
key: ScrollProportion.self,
value: value
)
.onPreferenceChange(ScrollProportion.self, perform: onChange)
}
)
}
}