Custom pressable button style
swiftui code
import SwiftUI
// MARK: - Custom pressable button style
struct PressableButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.scaleEffect(configuration.isPressed ? 0.9 : 1.0)
.shadow(
color: .black.opacity(0.3),
radius: configuration.isPressed ? 1 : 4,
x: 0,
y: configuration.isPressed ? 1 : 4
)
.animation(.easeInOut(duration: 0.15), value: configuration.isPressed)
}
}
// Basic view
struct ContentView: View {
@State var count = 0
var body: some View {
VStack {
Spacer()
HStack(spacing: 20) {
Button {
count += 1
} label: {
Image("increase-object-to-path")
.resizable()
.scaledToFit()
.frame(width: 80, height: 80)
}
.buttonStyle(PressableButtonStyle())
Button {
count -= 1
} label: {
Image("decrease-object-to-path")
.resizable()
.scaledToFit()
.frame(width: 80, height: 80)
}
.buttonStyle(PressableButtonStyle())
}
Text("People: \(count)")
.font(.largeTitle)
.bold()
.padding()
// aca
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.green.opacity(0.3))
.ignoresSafeArea()
}
}
#Preview {
ContentView()
}
Swift