Skip to content

Instantly share code, notes, and snippets.

@mdb1
Created February 1, 2023 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdb1/13df4fb33b6d3df119b89645214ca916 to your computer and use it in GitHub Desktop.
Save mdb1/13df4fb33b6d3df119b89645214ca916 to your computer and use it in GitHub Desktop.
Custom Progress View
import SwiftUI
public struct SpinnerProgressView: View {
@State private var isLoading = false
private let color: Color
private let size: CGFloat
private let lineWidth: CGFloat
public init(
color: Color = .accentColor,
size: CGFloat = 20,
lineWidth: CGFloat = 3
) {
self.color = color
self.size = size
self.lineWidth = lineWidth
}
public var body: some View {
Circle()
.trim(from: 0, to: 0.67)
.stroke(lineWidth: lineWidth)
.foregroundColor(color)
.frame(width: size, height: size)
.rotationEffect(Angle(degrees: isLoading ? 360 : 0))
.animation(
.linear.speed(0.75).repeatForever(autoreverses: false),
value: isLoading
)
.onAppear {
isLoading = true
}
}
}
struct SpinnerProgressView_Previews: PreviewProvider {
static var previews: some View {
VStack(spacing: 24) {
SpinnerProgressView()
SpinnerProgressView(color: .gray)
ZStack {
RoundedRectangle(cornerRadius: 8)
.frame(height: 60)
.foregroundColor(.accentColor)
SpinnerProgressView(color: .white)
}
Spacer()
}.padding()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment