Skip to content

Instantly share code, notes, and snippets.

@aheze
Created October 7, 2022 00:51
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/3e974316e29ce675fdcd0943d75f40e6 to your computer and use it in GitHub Desktop.
Save aheze/3e974316e29ce675fdcd0943d75f40e6 to your computer and use it in GitHub Desktop.
struct ScalingButtonStyle: ButtonStyle {
var scale = CGFloat(0.95)
func makeBody(configuration: Configuration) -> some View {
let animation: Animation? = {
if configuration.isPressed {
/// pressing down
return .spring(
response: 0.19,
dampingFraction: 0.45,
blendDuration: 1
)
} else {
return .spring(
response: 0.4,
dampingFraction: 0.4,
blendDuration: 1
)
}
}()
return configuration.label
.opacity(configuration.isPressed ? 0.95 : 1)
.scaleEffect(configuration.isPressed ? scale : 1)
.animation(animation, value: configuration.isPressed)
}
}
extension ButtonStyle where Self == ScalingButtonStyle {
static var scaling: ScalingButtonStyle {
ScalingButtonStyle()
}
static func scaling(_ scale: CGFloat) -> ScalingButtonStyle {
ScalingButtonStyle(scale: scale)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment