Improve startup UX with placeholders and prefetch
This commit is contained in:
51
Sources/Extensions/View+Shimmer.swift
Normal file
51
Sources/Extensions/View+Shimmer.swift
Normal file
@@ -0,0 +1,51 @@
|
||||
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) { isActive in
|
||||
if isActive {
|
||||
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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user