I'm trying to create a counter in order to randomize a number every each second.
As far, I get this code, but it activates automatically. It would be a pleasure to know how to activate this timer with a button.
My code:
struct BSR: View {
@State private var NumBSR = "---"
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
ZStack {
Image("BSR")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 62, height: 61, alignment: .center)
Text(String(NumBSR))
.font(.body)
.foregroundColor(.white)
.frame(width: 50, height: 40, alignment: .bottom)
}.onReceive(timer, perform: { time in
let Num = Int.random(in: 0...5)
NumBSR = String(Num)
})
}
}
Sorry if I can't write the code property, Im still learning. Thanks for your help.