51 lines
1.2 KiB
Swift
51 lines
1.2 KiB
Swift
import SwiftUI
|
|
|
|
struct ShimmerModifier: ViewModifier {
|
|
var active: Bool
|
|
@State private var phase: CGFloat = -1
|
|
|
|
func body(content: Content) -> some View {
|
|
content
|
|
.overlay(
|
|
shimmer
|
|
.mask(content)
|
|
.opacity(active ? 1 : 0)
|
|
)
|
|
.onAppear {
|
|
guard active else { return }
|
|
animate()
|
|
}
|
|
.onChange(of: active) {
|
|
guard active else { return }
|
|
phase = -1
|
|
animate()
|
|
}
|
|
}
|
|
|
|
private var shimmer: some View {
|
|
LinearGradient(
|
|
gradient: Gradient(colors: [
|
|
.clear,
|
|
Color.white.opacity(0.6),
|
|
.clear
|
|
]),
|
|
startPoint: .top,
|
|
endPoint: .bottom
|
|
)
|
|
.rotationEffect(.degrees(70))
|
|
.offset(x: phase * 250)
|
|
}
|
|
|
|
private func animate() {
|
|
withAnimation(.linear(duration: 1.2).repeatForever(autoreverses: false)) {
|
|
phase = 1
|
|
}
|
|
}
|
|
}
|
|
|
|
extension View {
|
|
func shimmering(active: Bool) -> some View {
|
|
modifier(ShimmerModifier(active: active))
|
|
}
|
|
}
|