I’m trying to implement a circular progress view that looks like so:
struct SpeedometerView: View {
var body: some View {
ZStack {
Color.black.edgesIgnoringSafeArea(.all)
speedProgressView(width: 300)
}
}
private func speedProgressView(width: CGFloat) -> some View {
ZStack {
Circle()
.trim(from: 0, to: 0.2)
.stroke(.red, style: StrokeStyle(lineWidth: 4, lineCap: .round))
.shadow(color: .green, radius: 10, x: 0, y: 0)
.shadow(color: .green, radius: 2, x: 0, y: 0)
}
.frame(width: width)
.rotationEffect(.degrees(100))
}
}
#Preview {
SpeedometerView()
}
Both ends are rounded as of now due to the stroke style. How do I trim/clip only the circle end that progresses clockwise unless progress is 100% ?
Any help is appreciated.
2
Answers
If you want only one end of the line to be .round and the other to be .butt. So you need Double Circle to do it. I hope it helps
I would write this as a
Shape
that is filled, instead of aPath
that is stroked.The path of the
Shape
is theunion
of:strokedPath
of a (trimmed) circular path, with the.butt
line cap.Example output:
For iOS 17 or earlier,
union
is not available, but you can just work withCGPath
s instead, and use itsunion
method instead.