@@ -11,6 +11,11 @@ struct CircularProgressView: View {
1111 var autoCompleteThreshold : Float ?
1212 var autoCompleteDuration : TimeInterval ?
1313
14+ var autoStartValue : Float ?
15+ var autoStartDuration : TimeInterval ?
16+
17+ @State private var currentProgress : Float = 0
18+
1419 var body : some View {
1520 ZStack {
1621 if let value {
@@ -19,13 +24,23 @@ struct CircularProgressView: View {
1924 . stroke ( backgroundColor, style: StrokeStyle ( lineWidth: strokeWidth, lineCap: . round) )
2025
2126 Circle ( )
22- . trim ( from: 0 , to: CGFloat ( displayValue ( for: value ) ) )
27+ . trim ( from: 0 , to: CGFloat ( displayValue ( for: currentProgress ) ) )
2328 . stroke ( primaryColor, style: StrokeStyle ( lineWidth: strokeWidth, lineCap: . round) )
2429 . rotationEffect ( . degrees( - 90 ) )
25- . animation ( autoCompleteAnimation ( for: value) , value: value)
2630 }
2731 . 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+ }
2944 } else {
3045 IndeterminateSpinnerView (
3146 diameter: diameter,
@@ -48,11 +63,15 @@ struct CircularProgressView: View {
4863 return value
4964 }
5065
51- private func autoCompleteAnimation ( for value: Float ) -> Animation ? {
66+ private func currentAnimation ( for value: Float ) -> Animation {
5267 guard let threshold = autoCompleteThreshold,
5368 let duration = autoCompleteDuration,
5469 value >= threshold, value < 1.0
5570 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+ }
5675 return . default
5776 }
5877
@@ -67,6 +86,13 @@ extension CircularProgressView {
6786 view. autoCompleteDuration = duration
6887 return view
6988 }
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+ }
7096}
7197
7298// We note a constant >10% CPU usage when using a SwiftUI rotation animation that
0 commit comments