I'm trying to change animation's duration while it animates. I've got two properties - isAnimating which determines whether animation should be running or not and duration. There is one view that is being animated, button which toggles isAnimating and Slider which is bound to duration state. When animation is playing and I change value using Slider, duration doesn't change. Is there any way to achieve what I want?
struct ContentView: View {
@State var isAnimating = false
@State var duration = 2.0
var body: some View {
VStack {
Spacer()
Circle()
.frame(width: 50.0, height: 50.0)
.scaleEffect(isAnimating ? 2.0 : 1.0)
.animation(
isAnimating ?
Animation.easeOut(duration: duration).repeatForever(autoreverses: false) :
.default
)
Spacer()
Button(
action: { self.isAnimating.toggle() },
label: { Text("Tap") }
)
Spacer()
Slider(value: $duration, in: (0.1 ... 5.0))
.padding([.horizontal], 20.0)
.padding([.bottom], 50.0)
}
}
}
I also tried different approaches but there was always some problem with it (animated view flickering for example).