Skip to content

Instantly share code, notes, and snippets.

@juliensagot
Last active June 28, 2025 21:07
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()
}
}
}
@juliensagot
Copy link
Author

juliensagot commented Nov 22, 2024

⚠️ Caution: If you’re embedding a SwiftUI view that uses buttons with this style inside a UIViewControllerRepresentable, those buttons’ actions will never be triggered.

@juliensagot
Copy link
Author

Do not use this snippet anymore, Apple finally fixed the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment