I've created a Loading view on swiftUI, using this code:
var body: some View {
ZStack {
Circle()
.trim(from: 0, to: 0.2)
.stroke(AngularGradient(gradient: .init(colors: [settings.colors[self.settings.accentColor]]), center: .center), style: StrokeStyle(lineWidth: 6, lineCap: .round))
.rotationEffect(Angle(degrees: isLoading ? 360 : 0))
Circle()
.trim(from: 0.2, to: 0.4)
.stroke(AngularGradient(gradient: .init(colors: [.red]), center: .center), style: StrokeStyle(lineWidth: 6, lineCap: .round))
.rotationEffect(Angle(degrees: isLoading ? -360 : 0))
.frame(height: aspect / 2)
Circle()
.trim(from: 0.5, to: 0.7)
.stroke(AngularGradient(gradient: .init(colors: [settings.colors[self.settings.accentColor]]), center: .center), style: StrokeStyle(lineWidth: 6, lineCap: .round))
.rotationEffect(Angle(degrees: isLoading ? 360 : 0))
Circle()
.trim(from: 0.7, to: 0.9)
.stroke(AngularGradient(gradient: .init(colors: [.red]), center: .center), style: StrokeStyle(lineWidth: 6, lineCap: .round))
.rotationEffect(Angle(degrees: isLoading ? -360 : 0))
.frame(height: aspect / 2)
.onAppear() {
self.isLoading = true
}
}
.animation(Animation.linear(duration: 0.5).repeatForever(autoreverses: false))
.frame(width: aspect, height: aspect, alignment: .center)
}
On the swiftui preview canvas everything seems fine:
But when run on simulator or real device, animation seems broken and it modifies view location while animating like this:
Anyone knows if this is a bug, or I'm missing something. Thanks in advance.