-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShimmeringText.swift
34 lines (32 loc) · 1.15 KB
/
ShimmeringText.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import SwiftUI
struct ShimmeringText: View {
var text: String
var baseColor: Color
var shimmerColor: Color = .white
@State private var gradientPosition: CGFloat = -1
var body: some View {
ZStack {
Text(text)
.foregroundColor(baseColor)
.overlay(
GeometryReader { geometry in
LinearGradient(
gradient: Gradient(colors: [shimmerColor.opacity(0), shimmerColor, shimmerColor.opacity(0)]),
startPoint: .leading,
endPoint: .trailing
)
.offset(x: gradientPosition * geometry.size.width)
.onAppear {
withAnimation(
Animation.linear(duration: 1.5)
.repeatForever(autoreverses: false)
) {
gradientPosition = 1
}
}
}
)
.mask(Text(text))
}
}
}