I have an array of string and of course when the view appears, the ForEach shows all the items at once, but I would like to show one item at a time, i think i should do it using an animation but i don’t know how to go on…
This is the effect i’m trying to achieve:
This is my simple code:
struct ContentView: View {
let correctNames = ["Steve", "Bill", "Elon", "Paul"]
var body: some View {
VStack {
ForEach(correctNames, id: .self) { name in
Text("(name)")
.font(.largeTitle)
.bold()
}
}
}
}
2
Answers
Simple approach.
Use a timer and a counter. Increment the counter every second and show the items 0 to counter animated. If the counter reaches the size of the array cancel the timer.
Here’s a different take. Use
.opacity()
to hide/show the names, and give each view an increasingdelay
. Whenanimating
is toggled withonAppear
, the views will show one by one.If you want to make the fade in longer, replace the
.animation()
line with:To increase or decrease the time between names, multiply
Double(index)
by the number of seconds you want.