I have a simple SwiftUI
macOS view that animates some text by altering the font size when tapped. It goes from a fixed value to another. The animation works perfectly, but I'd like to add an extra piece of interaction. In the view there's a hidden button linked to the spacebar. I'd like to print out the fontSize
at the moment in time when the spacebar gets pressed. If I print directly the fontSize
variable, I get the value at the end of the animation.
import SwiftUI
struct MeasureFontSizeView: View {
@State private var fontSize = 50.0
var body: some View {
ZStack {
Text("Hello!")
.font(.custom("Georgia", size: fontSize))
.frame(maxWidth: .infinity, maxHeight: .infinity)
Button {
// print out the font size
// at this stage of the animation
print(fontSize)
} label: {
Text("")
}
.buttonStyle(.borderless)
.keyboardShortcut(" ", modifiers: [])
}
.onTapGesture {
withAnimation(.linear(duration: 3)) {
fontSize = 200.0
}
}
}
}
Any advice?