我正在SwiftUI中创建一个类似于here的涟漪效果。
以下是我目前所掌握的情况:
import SwiftUI
// MARK: - Ripple
struct Ripple: ViewModifier {
// MARK: Lifecycle
init(rippleColor: Color) {
self.rippleColor = rippleColor
}
// MARK: Internal
let rippleColor: Color
func body(content: Content) -> some View {
ZStack {
content
if let location = touchPoint {
Circle()
.fill(rippleColor)
.frame(width: 16.0, height: 16.0)
.position(location)
.clipped()
.opacity(opacity)
}
}
.fixedSize()
.gesture(
DragGesture(minimumDistance: 0.0)
.onChanged { gesture in
guard touchPoint != gesture.startLocation else {
return
}
timer?.invalidate()
opacity = 1.0
touchPoint = gesture.startLocation
}
.onEnded { _ in
timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { _ in
withAnimation {
opacity = 0.0
}
}
}
)
}
// MARK: Private
@State private var opacity: CGFloat = 0.0
@State private var touchPoint: CGPoint?
@State private var timer: Timer?
}
extension View {
func rippleEffect(rippleColor: Color = .accentColor.opacity(0.5)) -> some View {
modifier(Ripple(rippleColor: rippleColor))
}
}
下一步是做缩放动画,但我很难弄清楚如何做。我试过用缩放修改器应用缩放效果和过渡,但似乎没有什么能正常工作。
有人能帮助我实现我所寻找的连锁React吗?
另外,如果已经存在这样的东西,我很乐意使用它,但我还没有找到任何东西。
谢谢你,
RPK公司
2条答案
按热度按时间rjzwgtxy1#
您可能正在寻找这样的东西...
ee7vknir2#
我用上面弗雷德里克的答案稍微修改了一下,以达到我想要的结果。
感谢弗雷德里克,为正确的方向推动。