Code: Select all
struct ScaledTappable: ViewModifier {
@State var state = false
var tapHandler: () -> Void
func body(content: Content) -> some View {
content
.scaleEffect(state ? 0.9 : 1)
.gesture(
DragGesture(minimumDistance: 0)
.onChanged({ value in
withAnimation(.smooth(duration: 0.2)) {
state = true
}
})
.onEnded({ value in
withAnimation(.bouncy(duration: 0.5)) {
state = false
tapHandler()
}
})
)
}
}
extension View {
@ViewBuilder
func tappable(enabled: Bool = true, onTap: @escaping () -> Void) -> some View {
if enabled {
self.modifier(ScaledTappable(tapHandler: onTap))
} else {
self.opacity(0.3)
}
}
}