Skip to content

Instantly share code, notes, and snippets.

@emrepun
Created October 10, 2024 12:19
Show Gist options
  • Save emrepun/26daadafa847de96d22b32fa36df9934 to your computer and use it in GitHub Desktop.
Save emrepun/26daadafa847de96d22b32fa36df9934 to your computer and use it in GitHub Desktop.
SwiftUI Easy Timers with Text
import SwiftUI
struct ContentView: View {
@State var startDate: Date = .now
@State var countDownEndDate: Date?
var body: some View {
VStack(spacing: 16.0) {
Text("Duration:")
.font(.title2)
Text(startDate, style: .timer)
.font(.title3)
Button {
countDownEndDate = Calendar.current.date(byAdding: .second, value: 600, to: .now)!
} label: {
Text("Start Countdown")
.font(.title2)
}
if let countDownEndDate {
let range = Date.now...countDownEndDate
Text(timerInterval: range, countsDown: false)
.font(.title3)
}
}
.padding()
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment