This animation runs as expected when first viewed but the timing gets messed up at random when the app is exited and then entered (or if you navigate away from the screen and back). How can this code be changed to keep the animation timing consistent after leaving the screen and coming back?
The error can be recreated by exiting and coming back to the app a couple times.
import SwiftUI
struct ExampleView: View {
@State var isAnimating: Bool = false
let timing = 4.0
let maxCounter: Int = 3
var body: some View {
ZStack {
Circle()
.stroke(
Color.blue.opacity(isAnimating ? 0.0 : 1.0),
style: StrokeStyle(lineWidth: isAnimating ? 0.0 : 15.0))
.scaleEffect(isAnimating ? 1.0 : 0.0)
.animation(
Animation.easeOut(duration: timing)
.repeatForever(autoreverses: false)
.delay(Double(0) * timing / Double(maxCounter) / Double(maxCounter)), value: isAnimating)
Circle()
.stroke(
Color.blue.opacity(isAnimating ? 0.0 : 1.0),
style: StrokeStyle(lineWidth: isAnimating ? 0.0 : 15.0))
.scaleEffect(isAnimating ? 1.0 : 0.0)
.animation(
Animation.easeOut(duration: timing)
.repeatForever(autoreverses: false)
.delay(Double(1) * timing / Double(maxCounter) / Double(maxCounter)), value: isAnimating)
Circle()
.stroke(
Color.blue.opacity(isAnimating ? 0.0 : 1.0),
style: StrokeStyle(lineWidth: isAnimating ? 0.0 : 15.0))
.scaleEffect(isAnimating ? 1.0 : 0.0)
.animation(
Animation.easeOut(duration: timing)
.repeatForever(autoreverses: false)
.delay(Double(2) * timing / Double(maxCounter) / Double(maxCounter)), value: isAnimating)
}
.frame(width: 200, height: 200, alignment: .center)
.onAppear {
isAnimating = true
}
}
}