Cool ripple effect on you SwiftUI view.
- easy to use for regular button
- long press and gesture support
Swift Package Manager:
https://github.com/Jnis/SwiftUIRippleEffect.git
- Make a shared RippleViewModel
- Add touch handler by .rippleTouchHandler method
- Add ripple effect view by .rippleEffect method
import SwiftUIRippleEffect
struct MyButton<V: View>: View {
@State private var rippleViewModel = RippleViewModel() // 1
let action: () -> Void
let label: () -> V
var body: some View {
VStack {
Button(action: {
action()
}, label: {
label()
.contentShape(Rectangle())
.rippleTouchHandler17AndOlder(viewModel: rippleViewModel) // 2 (iOS17 and older)
})
.buttonStyle(EmptyStyle())
.rippleTouchHandler(viewModel: rippleViewModel) // 2 (iOS18)
.background(
Capsule()
.foregroundColor(.yellow)
.rippleEffect(color: .gray,
rippleViewModel: rippleViewModel,
clipShape: Capsule()) // 3
)
}
}
}
You can find more examples inside /Examples
folder.
.rippleTouchHandler
must not be inside Button's label.rippleTouchHandler
must be afteronTapGesture
andonLongPressGesture
- use
.rippleTouchHandler17AndOlder
for old iOS versions.
MIT