此动画在第一次查看时按预期运行,但当应用退出然后进入(或者如果您离开屏幕并返回)时,时间会随机混乱。如何更改此代码以在离开屏幕并返回后保持动画时序一致?
可以通过退出并返回应用程序几次来重新创建错误。
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
}
}
}