Created
September 1, 2023 03:08
-
-
Save chriseidhof/8f30e82dff524a6fba05b5a14c2ece31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
import SwiftUI | |
struct MyProgress: View, Animatable { | |
var progress: Double | |
var animatableData: Double { | |
get { progress } | |
set { progress = newValue } | |
} | |
var lineWidth: CGFloat = 10 | |
var circleOpacity: CGFloat { | |
var start = 0.95 | |
return max(0, progress-start)/(1-start) | |
} | |
var body: some View { | |
let trimmed = Circle() | |
.rotation(.degrees(-90)) | |
.trim(from: 0, to: progress) | |
ZStack { | |
trimmed | |
.stroke(lineWidth: lineWidth) | |
Circle() | |
.frame(width: lineWidth, height: lineWidth) | |
.opacity(circleOpacity) | |
} | |
} | |
} | |
struct ContentView: View { | |
@State var progress: Double = 0 | |
var body: some View { | |
VStack { | |
Button("Toggle") { progress = progress == 0 ? 1 : 0} | |
Slider(value: $progress, in: 0...1) | |
MyProgress(progress: progress) | |
.animation(.easeInOut(duration: 3), value: progress) | |
} | |
} | |
} | |
#Preview { | |
ContentView() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment