按下按钮 swiftUI 时计时器不显示正确的时间

timer not show correct time when press button swiftUI

我正在创建一个计时器应用程序,在我使用按钮设置计时器的屏幕中,我想更改显示的字符串。在这种情况下,如果我按两次“10”第二个按钮,我只会看到 10 秒而不是 20 秒。

如何管理多个不同时间的按钮加在一起?我做了这个模型,但我认为它完全不正确,我不知道如何处理。

        @ObservedObject var tm: TimeManager
    
    var body: some View {
        VStack {
            ContdownView(text:tm.timerap, bgColor: .yellow)
                .cornerRadius(16)
                .padding()
                .shadow(radius: 2)
            
            VStack{
                Button(action: { self.tm.start() }) {
                    ButtonView(text: "10 SEC", bgColor: .red)
                        .cornerRadius(16)
                        .padding(.leading, 16)
                }
                Button(action: { self.tm.stop() }) {
                    ButtonView(text: "STOP", bgColor: .red)
                        .cornerRadius(16)
                        .padding(.leading, 16)
                }
            }
        }
    }


class TimeManager:ObservableObject {
        
    var timer = Timer()
        
    var timerap: String = "00:00:00" {
        didSet {
            self.objectWillChange.send()
        }
    }
    
    func start() {
        if !timer.isValid {
            timer = Timer.scheduledTimer(withTimeInterval: 0.0,
                                         repeats: false) { timer in
                self.tick()
            }
        }
    }
    
    func tick() {
        /// aumentiamo il contatore
        timerap = "00:00:10"
    }
    
    func stop() {
        if timer.isValid {
            timer.invalidate()
        } else {
            contatore = 0
            timerap = "00:00:00"
        }
    }
    
    func pausa() {
        if timer.isValid {
            timer.invalidate()
        }
    }
    
    func timeString(_ time:TimeInterval) -> String {
        let hours = Int(time) / 3600
        let minutes = Int(time) / 60
        let seconds = time - Double(minutes) * 60
        return String(format:"%02i:%02i:%02i",hours,minutes,Int(seconds))
    }
}

您应该每次都添加秒数,但您每次都只是在尝试创建计时器对象。

Here is a good example for you. You can start here and refactor it depending on your requests.

import SwiftUI

struct ContentView: View {
    @State var isCounting = false
    @State var remainingSeconds = 10
    let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
    
    var body: some View {
        VStack {
            Text("\(timeString(from: remainingSeconds))")
                .onReceive(timer) { _ in
                    if isCounting && remainingSeconds > 0 {
                        remainingSeconds -= 1
                    }
                }
        }.padding()
        
        VStack {
            HStack {
                Button("+10 SEC") {
                    remainingSeconds += 10
                }.padding()
                Button("+20 SEC") {
                    remainingSeconds += 20
                }.padding()
            }.padding()
            
            HStack {
                Button("START") {
                    isCounting = true
                }.padding()
                Button("STOP") {
                    isCounting = false
                }.padding()
            }.padding()
        }
    }
    
    private func timeString(from totalSeconds: Int) -> String {
        let hours = totalSeconds / 3600
        let minutes = (totalSeconds % 3600) / 60
        let seconds = totalSeconds % 60
        return String(format:"%02i:%02i:%02i",hours,minutes, seconds)
    }
}

显然,您应该使用 Swift 的 DateFormatter

来格式化您的日期

这里有一些资源供您使用:

How to use a timer with SwiftUI

Working with dates