Skip to content

Instantly share code, notes, and snippets.

@aheze
Created October 7, 2022 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheze/ec9df5b7604377b763f75c4978a492d9 to your computer and use it in GitHub Desktop.
Save aheze/ec9df5b7604377b763f75c4978a492d9 to your computer and use it in GitHub Desktop.
struct PressedButtonStyle: ButtonStyle {
@Binding var isPressed: Bool
func makeBody(configuration: Configuration) -> some View {
var animation: Animation?
/// only change when it's different
if isPressed != configuration.isPressed {
if configuration.isPressed {
animation = .spring(
response: 0.1,
dampingFraction: 0.6,
blendDuration: 1
)
} else {
animation = .spring(
response: 0.4,
dampingFraction: 0.4,
blendDuration: 1
)
}
DispatchQueue.main.async {
withAnimation(animation) {
isPressed = configuration.isPressed
}
}
}
return configuration.label
.opacity(1) /// needs a modifier to actually be called
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment