I have a list of strings I want to show to the user, sequentially. For example:
var array = ["Hey there!", "What's your name?"]
What I specifically want, is to show the first string for a few seconds and when that time passes, show the next.
Here's what I've done so far:
struct ContentView: View {
@State private var array = ["Hey there!", "What's your name?"]
var body: some View {
VStack {
TimedTextView(text: array.first, numberOfVisibilitySeconds: 3, onFinishedShowing: {
self.array.removeFirst()
})
}
}
}
And here’s TimedTextView:
struct TimedTextView: View {
@State private var shouldMakeTextVisible = true
var text: String!
var numberOfVisibilitySeconds: Double!
var onFinishedShowing: (() -> Void)!
var body: some View {
VStack {
if shouldMakeTextVisible {
Text(text)
.font(.largeTitle)
.transition(.asymmetric(insertion: .opacity, removal: .opacity))
.animation(.easeIn)
.onAppear(perform: {
DispatchQueue.main.asyncAfter(deadline: .now() + numberOfVisibilitySeconds) {
withAnimation {
self.shouldMakeTextVisible.toggle()
}
self.onFinishedShowing()
}
})
}
}
}
}
As you can see, I'm using a closure to inform ContentView that the Text is done showing and to get the next one. But, what actually happens is that I see only the first string... Any idea what I’m doing wrong?