My desired outcome is to cover values and ranges between 0% and 100%, but my progress view works with values of 0.0 to 1.0 where 1.0 equal 100%
Can someone point me to the right direction?
import SwiftUI
struct ProgressBar: View {
@Binding var progress: Double
var color: Color
init(progress: Binding<Double>) {
_progress = progress
switch progress.wrappedValue {
case 0.0...0.20: // Fix: on 0.206 I get black color value
color = .green
case 0.21...0.40:
color = .green
case 0.41...0.60:
color = .yellow
case 0.61...1.0:
color = .red
default:
color = .black
}
}
CodePudding user response:
As you've discovered, your cases don't cover all of the numerical possibilities. Also, your color
should likely be a computed property based on the current state of the Binding
, rather than something you set in init
:
struct ProgressBar: View {
@Binding var progress: Double
var color: Color {
switch progress {
case 0.0..<0.20:
return .blue
case 0.20..<0.40:
return .green
case 0.40..<0.60:
return .yellow
case 0.60...1.0:
return .red
default:
return .black
}
}
var body: some View {
color
}
}