SwiftUI linear gauge

Viewed 182

UPDATE

I was able to get something closer to what I'm trying to achieve. Still needs work. I attempted to use a rounded rectangle for the mask but doesn't look good. Any suggestions would be greatly appreciated.

enter image description here

GeometryReader { geometry in
    RoundedRectangle(cornerRadius: 7.5, style: .circular)
        .fill(LinearGradient(gradient: Gradient(colors: [Color.green, Color.yellow, Color.orange, Color.red, Color.purple]), startPoint: .bottom, endPoint: .top))
        .frame(width: geometry.size.width, height: geometry.size.height, alignment: .bottomLeading)
        .overlay(Color.black.opacity(0.35).cornerRadius(7.5))
    RoundedRectangle(cornerRadius: 7.5, style: .circular)
        .fill(LinearGradient(gradient: Gradient(colors: [Color.green, Color.yellow, Color.orange, Color.red, Color.purple]), startPoint: .bottom, endPoint: .top))
        .frame(width: geometry.size.width, height: geometry.size.height, alignment: .bottomLeading)
        .mask(
            VStack {
                Spacer()
                Rectangle()
                    // Adjust value 1 to needs
                    .frame(width: geometry.size.width, height:geometry.size.height * (CGFloat(1) / CGFloat(11)), alignment: .bottom)
            })
}
.frame(width: 15, height: .infinity)
.padding(.all, 10)

Older

I'm attempting to create a simple gauge like the linear gauge in watchOS. The trouble I'm having is figuring out the proper way to overlay the current value. The outcome I'd like to see is below. I'm sure there's a better way.

Outcome

enter image description here

Mine look absolutely terrible

enter image description here

ZStack {
    LinearGradient(gradient: Gradient(colors: [Color.green, Color.yellow, Color.orange, Color.red, Color.purple]), startPoint: .bottom, endPoint: .top)
    GeometryReader { metrics in
        Circle()
            .stroke(Color.white,lineWidth: 2)
            .frame(minHeight: 0, maxHeight: metrics.size.height)
            .foregroundColor(Color.clear)
            .position(x: metrics.size.width / 2 ,y: metrics.size.height * (CGFloat(10) / CGFloat(11)))
    }
}
.cornerRadius(7.5)
.frame(minWidth: 0, maxWidth: 15)
1 Answers

I've solved my solution. I managed to create the same view by making a custom ProgressView style.

enter image description here

Related