Skip to content

Instantly share code, notes, and snippets.

@StewartLynch
Last active December 30, 2023 19:09
Show Gist options
  • Save StewartLynch/91dd614c41677bf99b73439ed1c548fc to your computer and use it in GitHub Desktop.
Save StewartLynch/91dd614c41677bf99b73439ed1c548fc to your computer and use it in GitHub Desktop.
ContentTransition
import SwiftUI
struct ContentView: View {
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
@State private var totalSeconds = 30
var body: some View {
Text(timeStringFromSeconds(totalSeconds))
.monospaced()
.contentTransition(.numericText())
.font(.system(size: 60))
.bold()
.onReceive(timer) { input in
withAnimation {
totalSeconds -= 1
}
}
}
func timeStringFromSeconds(_ totalSeconds: Int) -> String {
let minutes = totalSeconds / 60
let seconds = totalSeconds % 60
return String(format: "%01d:%02d", minutes, seconds)
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment