|
5 | 5 | import SwiftUI |
6 | 6 |
|
7 | 7 | struct Shimmer: ViewModifier { |
8 | | - @State private var phase: CGFloat = 0 |
9 | | - var duration = 1.5 |
10 | | - var delay = 0.25 |
| 8 | + /// The duration of a shimmer cycle in seconds. Default: `1.5`. |
| 9 | + var duration: Double = 1.5 |
| 10 | + /// The delay until the animation re-starts. |
| 11 | + var delay: Double = 0.25 |
| 12 | + |
| 13 | + @State private var isInitialState = true |
11 | 14 |
|
12 | 15 | public func body(content: Content) -> some View { |
13 | 16 | content |
14 | | - .modifier(AnimatedMask(phase: phase)) |
15 | | - .animation( |
16 | | - .linear( |
17 | | - duration: duration |
| 17 | + .mask( |
| 18 | + LinearGradient( |
| 19 | + gradient: .init(colors: [.black.opacity(0.4), .black, .black.opacity(0.4)]), |
| 20 | + startPoint: (isInitialState ? .init(x: -0.3, y: -0.3) : .init(x: 1, y: 1)), |
| 21 | + endPoint: (isInitialState ? .init(x: 0, y: 0) : .init(x: 1.3, y: 1.3)) |
18 | 22 | ) |
19 | | - .delay(delay) |
20 | | - .repeatForever( |
21 | | - autoreverses: false |
22 | | - ), |
23 | | - value: phase == 0.0 |
24 | 23 | ) |
25 | | - .onAppear { phase = 0.8 } |
26 | | - } |
27 | | - |
28 | | - /// An animatable modifier to interpolate between `phase` values. |
29 | | - struct AnimatedMask: AnimatableModifier { |
30 | | - var phase: CGFloat = 0 |
31 | | - |
32 | | - var animatableData: CGFloat { |
33 | | - get { phase } |
34 | | - set { phase = newValue } |
35 | | - } |
36 | | - |
37 | | - func body(content: Content) -> some View { |
38 | | - content |
39 | | - .mask(GradientMask(phase: phase).scaleEffect(3)) |
40 | | - } |
41 | | - } |
42 | | - |
43 | | - /// A slanted, animatable gradient between transparent and opaque to use as mask. |
44 | | - /// The `phase` parameter shifts the gradient, moving the opaque band. |
45 | | - struct GradientMask: View { |
46 | | - let phase: CGFloat |
47 | | - let centerColor = Color.black |
48 | | - let edgeColor = Color.black.opacity(0.3) |
49 | | - |
50 | | - var body: some View { |
51 | | - LinearGradient( |
52 | | - gradient: |
53 | | - Gradient(stops: [ |
54 | | - .init(color: edgeColor, location: phase), |
55 | | - .init(color: centerColor, location: phase + 0.1), |
56 | | - .init(color: edgeColor, location: phase + 0.2) |
57 | | - ]), |
58 | | - startPoint: .topLeading, |
59 | | - endPoint: .bottomTrailing |
| 24 | + .animation( |
| 25 | + .linear(duration: duration) |
| 26 | + .delay(delay) |
| 27 | + .repeatForever(autoreverses: false), |
| 28 | + value: isInitialState |
60 | 29 | ) |
61 | | - } |
| 30 | + .onAppear { |
| 31 | + isInitialState = false |
| 32 | + } |
62 | 33 | } |
63 | 34 | } |
64 | 35 |
|
| 36 | + |
65 | 37 | extension View { |
66 | 38 | /// Adds an animated shimmering effect to any view, typically to show that |
67 | 39 | /// an operation is in progress. |
|
0 commit comments