Skip to content

Instantly share code, notes, and snippets.

@juliensagot
Last active October 6, 2024 22:58
Show Gist options
  • Save juliensagot/efbebfd5e1716893e5b4b78abb9919b1 to your computer and use it in GitHub Desktop.
Save juliensagot/efbebfd5e1716893e5b4b78abb9919b1 to your computer and use it in GitHub Desktop.
Custom ButtonStyle boilerplate that doesn't take ages to update its `isPressed` state when using spring animations
struct ResponsiveButtonStyle: PrimitiveButtonStyle {
// Use this instead of `Configuration.isPressed`.
@State private var isPressed = false
func makeBody(configuration: Configuration) -> some View {
configuration.label
.scaleEffect(isPressed ? 0.9 : 1)
.animation(.spring, value: isPressed)
._onButtonGesture { isPressed in
self.isPressed = isPressed
} perform: {
configuration.trigger()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment