Last active
December 30, 2023 19:09
-
-
Save StewartLynch/91dd614c41677bf99b73439ed1c548fc to your computer and use it in GitHub Desktop.
ContentTransition
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 { | |
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