SwiftUI-如何将秒格式化为分:秒?

bttbmeg0  于 2022-11-28  发布在  Swift
关注(0)|答案(1)|浏览(201)

我尝试在SwiftUI中制作一个计时器,目前为止一切正常,但我不知道如何将秒格式化为秒和分。因此,例如,如果倒计时是299秒,我希望它显示4(分):59(秒),而不是300秒。
我在youtube上看到了一个视频,也制作了一个计时器,他们设法通过制作分钟和秒常数来格式化它,然后通过一个函数来格式化它,但它对我并不真正起作用。

import SwiftUI

struct TimerView: View {
    var timerEnd = 0
    @State var countdownTimer = 300
    @State var timerRunning = false
    @State var isPaused = false
    @State var isActive = false
    @State var showingAlert = false
    let timer = Timer.publish(every: 1, tolerance: 0.5, on: .main, in: .common).autoconnect()
    
    func format(result: Int) -> String {
        let value = String(format:"%d:%02\(300/60)", countdownTimer)
        return value
    }
    
    func reset() {
        countdownTimer = 300
        timerRunning = true
    }
    
    
    
    var body: some View {
        VStack {
            Text("Time: \(format(result: countdownTimer))")
                .padding()
                .onReceive(timer) { _ in
                    if countdownTimer > 0 && timerRunning {
                        countdownTimer -= 1
                    } else {
                        timerRunning = false
                    }
                    
                }
                .font(.system(size: 30))
            
            HStack(spacing:30) {
                Button(timerRunning ? "Reset" : "Start") {
                    reset()
                }
                .padding()
                .background((Color(red: 184/255, green: 243/255, blue: 255/255)))
                .foregroundColor(.black)
                .cornerRadius(10)
                .font(Font.system(size: UIFontMetrics.default.scaledValue(for: 16)))
                
                Button(timerRunning ? "Pause" : "Resume") {
                    if timerRunning == true {
                        timerRunning = false
                    } else {
                        timerRunning = true
                    }
                    
                }
                .padding()
                .foregroundColor(.black)
                .background(.red)
                .cornerRadius(10)
                .font(Font.system(size: UIFontMetrics.default.scaledValue(for: 16)))
            }
        }
    }
}

struct TimerView_Previews: PreviewProvider {
    static var previews: some View {
        TimerView()
    }
}
tzdcorbm

tzdcorbm1#

你可以这样做一个扩展类。在这段代码之后,你会在你的int值中看到一个方法。
您可以使用递减计数计时器。convertDurationToString()

extension Int {
    
    public func hmsFrom() -> (Int, Int, Int) {
        return (self / 3600, (self % 3600) / 60, (self % 3600) % 60)
    }
    
    public func convertDurationToString() -> String {
        var duration = ""
        let (hour, minute, second) = self.hmsFrom()
        if (hour > 0) {
            duration = self.getHour(hour: hour)
        }
        return "\(duration)\(self.getMinute(minute: minute))\(self.getSecond(second: second))"
    }
    
    private func getHour(hour: Int) -> String {
        var duration = "\(hour):"
        if (hour < 10) {
            duration = "0\(hour):"
        }
        return duration
    }
    
    private func getMinute(minute: Int) -> String {
        if (minute == 0) {
            return "00:"
        }

        if (minute < 10) {
            return "0\(minute):"
        }

        return "\(minute):"
    }
    
    private func getSecond(second: Int) -> String {
        if (second == 0){
            return "00"
        }

        if (second < 10) {
            return "0\(second)"
        }
        return "\(second)"
    }
}

相关问题