I created a custom horizontal scroll, which uses animation to scroll. When the view is presented, it uses animation to present the views.
So when I invoke the view and the onAppear is initiated, it changes the offset with animation, But this behavior is unwanted. I don't want to animate my view when they are created.
I tried to create @State variable
@State private var myBool = false
and create a condition:
.animation(self.myBool ? Animation.spring : .none)
and then inside .onAppear
.onAppear(
...
some code that changing state variables
...
self.myBool = true
)
but it didn't work, it still uses the animation even when myBool is false
How can I allow animation only AFTER the body is created? I mean, is there a modifier .onFinish?
This is an example of the code:
struct scroll: View {
@State var offset: 0
var body: some View {
GeometryReader { geo in
HStack {
ForEach(0..<5) { i in
Rectangle()
}
}
.gesture(DragGesture()
.onChange ({ (value) in
self.offset = value.translation.width
}
.onEnded ({ _ in
self.offset = 50
}
)
.offset(x: self.offset)
.animation(.spring)
.onAppear (
self.offset = geo.size.width
)
}
}
}