fankibiber
fankibiber

Reputation: 809

How to stop timer in Text view?

It is possible to pass a date to Text() in SwiftUI, then format it as a timer using the style argument. However, a countdown like this never stops, it just keeps incrementing after zero. How to make it stop at 0?

func nextRollTime(in seconds: Int) -> Date {
    let date = Calendar.current.date(byAdding: .second, value: seconds, to: Date())
    return date ?? Date()
}

Above is the function I use to start a countdown, then I pass it as follows:

Text(nextRollTime(in: 20), style: .timer)

Upvotes: 2

Views: 1595

Answers (2)

soundflix
soundflix

Reputation: 2763

iOS16+ / macOS 13+

Text now has a dedicated initializer for this, init(timerInterval:pauseTime:countsDown:showsHours:).

You may use it like this:

let date = Date()
Text(timerInterval: date...date.addingTimeInterval(9), countsDown: true)

Upvotes: 4

Asperi
Asperi

Reputation: 258117

Here is a demo of possible approach - as .timer run from now for ever (by design), the idea is to replace it with regular text once specified period is over.

Tested with Xcode 12b3 / iOS 14.

demo

struct DemoView: View {
    @State private var run = false

    var body: some View {
        VStack {
            if run {
                Text(nextRollTime(in: 10), style: .timer)
            } else {
                Text("0:00")
            }
        }
        .font(Font.system(.title, design: .monospaced))
        .onAppear {
            self.run = true
        }
    }

    func nextRollTime(in seconds: Int) -> Date {
        let date = Calendar.current.date(byAdding: .second, value: seconds, to: Date())
        DispatchQueue.main.asyncAfter(deadline: .now() + Double(seconds)) {
            self.run = false
        }
        return date ?? Date()
    }
}

Upvotes: 5

Related Questions