Post not yet marked as solved
Post marked as unsolved with 0 replies, 795 views
I would like to create an animation that repeats forever and depends on a random variable. Something like the following:import SwiftUI
struct ContentView: View {
@State var resize = 100
var repeatingAnimation : Animation {
return Animation.default.repeatForever()
}
var body: some View {
VStack {
Spacer()
RoundedRectangle(cornerRadius: 10)
.frame(width: 40, height: CGFloat(resize))
.onAppear() {
withAnimation(self.repeatingAnimation) {
self.resize = Int.random(in: 100 ... 400)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}However the random in line 17 is only executed on initial load/appear. I was hoping that "resize" would change every time the animation repeats, so that the height of the RoundedRectangle would fluctuate/change.How can this be done?Kind regards,Anders Sejersbøl