Created
February 14, 2023 10:25
-
-
Save scalbatty/9a10cfbc71de156245ff04371aaebf07 to your computer and use it in GitHub Desktop.
A "rolling counter" style label
This file contains 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
struct AnimatedCounter: Animatable, View { | |
var value: CGFloat = 0 | |
var maxOffset: CGFloat = 12 | |
var animatableData: CGFloat { | |
get { value } | |
set { value = newValue } | |
} | |
var progress: CGFloat { | |
value - floor(value) | |
} | |
var body: some View { | |
ZStack { | |
if value.rounded() == value { // Integer, just show the text label | |
Text("\(Int(value))") | |
} else { | |
Text("\(Int(floor(value)))") | |
.offset(y: progress * -maxOffset) | |
.opacity(1.0 - progress) | |
Text("\(Int(ceil(value)))") | |
.offset(y: maxOffset + progress * -maxOffset) | |
.opacity(progress) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment