Skip to content

Instantly share code, notes, and snippets.

@waterlou
Created March 14, 2021 04:05
Show Gist options
  • Save waterlou/d5ed8b6bed2d1f2ee77f1b1537dcbd91 to your computer and use it in GitHub Desktop.
Save waterlou/d5ed8b6bed2d1f2ee77f1b1537dcbd91 to your computer and use it in GitHub Desktop.
MaterialButtonStyle
public struct MaterialButtonStyle: ButtonStyle {
public let color: MaterialColor
public let cornerRadius: CGFloat
public let borderWidth: CGFloat
public let maxWidth: Bool
public func makeBody(configuration: Configuration) -> some View {
configuration
.label
.frame(minWidth: 44.0, maxWidth: maxWidth ? .infinity : nil, minHeight: 44.0)
.padding([.leading, .trailing], 16.0)
.foregroundColor(color.textColor)
.background(
GeometryReader { metrics in
let scale = max(metrics.size.width / metrics.size.height, metrics.size.height / metrics.size.width) * 1.1
ZStack {
// Solid fill
RoundedRectangle(cornerRadius: cornerRadius).fill(color.color).shadow(color: Color(.sRGB, white: 0, opacity: 0.3), radius: (configuration.isPressed ? 8 : 2), x: 0, y: 2)
// tap effect
Circle().fill(Color.white).scaleEffect(configuration.isPressed ? scale : 0.0001).opacity(configuration.isPressed ? 0.6 : 0.0).cornerRadius(cornerRadius)
}
}
)
.overlay(
// border
RoundedRectangle(cornerRadius: cornerRadius).stroke(color.color, lineWidth: borderWidth).opacity(0.2)
)
.animation(.easeInOut(duration: 0.15))
}
public init(color: MaterialColor = .accent, cornerRadius: CGFloat = 4.0, borderWidth: CGFloat = 1.0, maxWidth: Bool = false) {
self.color = color
self.cornerRadius = cornerRadius
self.borderWidth = borderWidth
self.maxWidth = maxWidth
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment