ios – Why I can’t make a timer with swift?


import SwiftUI



struct TestGameView: View {
    
    @State var countDownTimer = 6
    @State var timerRunning = false
    let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
    
    var body: some View {
        
        VStack{
            Text("\(countDownTimer)")
                .onReceive(timer){ _ in
                    if countDownTimer > 0 && timerRunning {
                        countDownTimer -= 1
                    } else {
                        timerRunning = false
                    }
                }
        }
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        TestGameView()
    }
}

I have to make a basic app and I actually make with storyboard but I can’t remember how I make. The purpose of the application is to have an image appear and disappear every second at a random location on the screen for 60 seconds, and when the image is touched while visible, the user earns points.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img