@@ -11,6 +11,11 @@ struct CircularProgressView: View {
11
11
var autoCompleteThreshold : Float ?
12
12
var autoCompleteDuration : TimeInterval ?
13
13
14
+ var autoStartValue : Float ?
15
+ var autoStartDuration : TimeInterval ?
16
+
17
+ @State private var currentProgress : Float = 0
18
+
14
19
var body : some View {
15
20
ZStack {
16
21
if let value {
@@ -19,13 +24,23 @@ struct CircularProgressView: View {
19
24
. stroke ( backgroundColor, style: StrokeStyle ( lineWidth: strokeWidth, lineCap: . round) )
20
25
21
26
Circle ( )
22
- . trim ( from: 0 , to: CGFloat ( displayValue ( for: value ) ) )
27
+ . trim ( from: 0 , to: CGFloat ( displayValue ( for: currentProgress ) ) )
23
28
. stroke ( primaryColor, style: StrokeStyle ( lineWidth: strokeWidth, lineCap: . round) )
24
29
. rotationEffect ( . degrees( - 90 ) )
25
- . animation ( autoCompleteAnimation ( for: value) , value: value)
26
30
}
27
31
. frame ( width: diameter, height: diameter)
28
-
32
+ . onAppear {
33
+ if let autoStartValue, let autoStartDuration, value == 0 {
34
+ withAnimation ( . easeOut( duration: autoStartDuration) ) {
35
+ currentProgress = autoStartValue
36
+ }
37
+ }
38
+ }
39
+ . onChange ( of: value) {
40
+ withAnimation ( currentAnimation ( for: value) ) {
41
+ currentProgress = value
42
+ }
43
+ }
29
44
} else {
30
45
IndeterminateSpinnerView (
31
46
diameter: diameter,
@@ -48,11 +63,15 @@ struct CircularProgressView: View {
48
63
return value
49
64
}
50
65
51
- private func autoCompleteAnimation ( for value: Float ) -> Animation ? {
66
+ private func currentAnimation ( for value: Float ) -> Animation {
52
67
guard let threshold = autoCompleteThreshold,
53
68
let duration = autoCompleteDuration,
54
69
value >= threshold, value < 1.0
55
70
else {
71
+ // Use the auto-start animation if it's running, otherwise default.
72
+ if let autoStartDuration, value < ( autoStartValue ?? 0 ) {
73
+ return . easeOut( duration: autoStartDuration)
74
+ }
56
75
return . default
57
76
}
58
77
@@ -67,6 +86,13 @@ extension CircularProgressView {
67
86
view. autoCompleteDuration = duration
68
87
return view
69
88
}
89
+
90
+ func autoStart( to value: Float , duration: TimeInterval ) -> CircularProgressView {
91
+ var view = self
92
+ view. autoStartValue = value
93
+ view. autoStartDuration = duration
94
+ return view
95
+ }
70
96
}
71
97
72
98
// We note a constant >10% CPU usage when using a SwiftUI rotation animation that
0 commit comments