Timers in SwiftUI
I learned how to use timers in SwiftUI. For example, I want to display a launcher view in 3 seconds and then proceed to the next View()
.
@State private var timerCount = 0
@State private var exitFromLauncher = false
private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
In our view:
var body: some View {
if exitFromLauncher {
// go to HomeView()
} else {
// Launcher UI
ZStack {
}
.onReceive(timer) { input in
if timerCount < 3 {
timerCount += 1
}
if timerCount == 3 {
exitFromLauncher = true
timer.upstream.connect().cancel()
}
}
}
}