Created
October 10, 2024 12:19
-
-
Save emrepun/26daadafa847de96d22b32fa36df9934 to your computer and use it in GitHub Desktop.
SwiftUI Easy Timers with Text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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