Created
February 1, 2023 18:05
-
-
Save mdb1/13df4fb33b6d3df119b89645214ca916 to your computer and use it in GitHub Desktop.
Custom Progress View
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 | |
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