Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create SwiftUI Text with timer interval starting from given duration up?

Tags:

ios

timer

swiftui

My simple timer is defined as:

let now = Date.now
var start = Calendar.current.date(byAdding: .second, value: Int(16), to: now)!
var end = Calendar.current.date(byAdding: .second, value: Int(16), to: start)!
 
Text(timerInterval: start...end, countsDown: false)

I was expecting to start counting it like: 00:16, 00:17, 00:18 ... 00:32.

But it works like 00:00, 00:01 ... 00:16 and starts counting 16 seconds after load.

How can I start it from given interval and end on a given interval from future?

like image 384
Bartłomiej Semańczyk Avatar asked Sep 17 '25 21:09

Bartłomiej Semańczyk


1 Answers

This should achieve what you'd like. You need to subtract 16 from now so the timer knows to start from 16s in the past. The timer is always relative to now.

let now = Date.now
var start = Calendar.current.date(byAdding: .second, value: Int(-16), to: now)!
var end = Calendar.current.date(byAdding: .second, value: Int(16), to: start)!
 
Text(timerInterval: start...end, countsDown: false)
like image 71
Kyle Venn Avatar answered Sep 19 '25 20:09

Kyle Venn