为你的 SwiftUI 视图添加酷炫的波纹效果。
![]() |
Swift Package Manager
https://github.com/Jnis/SwiftUIRippleEffect.git
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
)
}
}
}
你可以在 /Examples
文件夹中找到更多示例。
.rippleTouchHandler
不得在 Button 的 label 内部.rippleTouchHandler
必须在 onTapGesture
和 onLongPressGesture
之后.rippleTouchHandler17AndOlder
。MIT