I would like to create a view with a gradient and an infinite loading like animation (the darkest part of the gradient would start from the left of the view, then take 2 seconds to animate to the right, and as it "exits" the view on the right, it would appear again on its left).
A visual example (not animated, sorry) :
Here is what I started writing but it doesn't work as I want.
struct TestGradientView: View {
@State private var animateGradient = false
var body: some View {
LinearGradient(
colors: [
.gray.opacity(0.25),
.gray.opacity(0.5)
],
startPoint: animateGradient ? .leading : .trailing,
endPoint: !animateGradient ? .leading : .trailing
)
.onAppear {
withAnimation(
.linear(duration: 2.0)
.repeatForever(autoreverses: true)
) {
animateGradient.toggle()
}
}
}
}
Use:
VStack {
Spacer()
TestGradientView()
.cornerRadius(4.0)
.padding(.horizontal, 16)
.frame(height: 8)
Spacer()
}

