Created
July 29, 2024 03:48
-
-
Save Koshimizu-Takehito/f589657bcf26474bff1a840455b5c695 to your computer and use it in GitHub Desktop.
ProgressRing
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 ContentView: View { | |
@State var progress = 0.0 | |
var body: some View { | |
ProgressRing(value: progress) | |
.frame(maxWidth: .infinity, maxHeight: .infinity) | |
.contentShape(.rect) | |
.ignoresSafeArea() | |
.onTapGesture(perform: refresh) | |
} | |
func refresh() { | |
progress = 0 | |
withAnimation(.easeOut(duration: 3)) { | |
progress = 1 | |
} | |
} | |
} | |
struct ProgressRing: View, Animatable { | |
var value = 0.0 | |
var animatableData: Double { | |
get { max(min(value, 1), 0) } | |
set { value = max(min(newValue, 1), 0) } | |
} | |
var body: some View { | |
ZStack { | |
Circle() | |
.stroke(lineWidth: 15) | |
.foregroundStyle(.gray.opacity(0.3)) | |
Circle() | |
.trim(from: 0, to: value) | |
.stroke(style: StrokeStyle(lineWidth: 18, lineCap: .round, lineJoin: .round)) | |
.foregroundStyle( | |
LinearGradient(colors: [.cyan, .blue], startPoint: .top, endPoint: .bottom) | |
) | |
.rotationEffect(.degrees(-90.0 + (360.0 * value))) | |
HStack(alignment: .bottom, spacing: 0) { | |
Text("\(Int(value * 100))") | |
.font(.largeTitle) | |
Text("%") | |
.padding(.bottom, 5) | |
} | |
.bold() | |
.monospacedDigit() | |
.foregroundStyle(.foreground) | |
} | |
.frame(width: 150, height: 150) | |
} | |
} | |
#Preview { | |
ContentView() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment