This view is a UIKit slider adapted to my SwiftUI project because in SwiftUI Slider
cannot change its track color, which is probably a bug since you should be able to change it with .accentColor
.
Anyway, this slider changes its track color according to its value, from green to red. The whole thing works perfectly (although my gradients aren't that good yet) if value
is bound to a normal @State
property, but the second you try to attach it to a property of an @ObservedObject
it breaks and, although the track color still works, it never changes the underlying value. I would like to think that this is just a bug right now but it's more likely there's something here that needs to be fixed.
struct RedscaleSlider: UIViewRepresentable {
@Binding var value: Double
var min: Double
var max: Double
class Coordinator: NSObject {
@Binding var value: Double
var min: Double
var max: Double
init(value: Binding<Double>, min: Double = 0, max: Double = 100) {
_value = value
self.min = min
self.max = max
}
@objc func valueChanged(_ sender: UISlider) {
self.value = Double(sender.value)
sender.minimumTrackTintColor = green_to_red_gradient(value: (Double(sender.value) - min) / (max - min)).into_UIKit_color()
}
}
var thumb_color: UIColor = .white
var track_color: UIColor = .systemBlue
func makeUIView(context: Context) -> UISlider {
let slider = UISlider(frame: .zero)
slider.addTarget(
context.coordinator,
action: #selector(Coordinator.valueChanged),
for: .valueChanged
)
slider.thumbTintColor = thumb_color
slider.minimumTrackTintColor = track_color
slider.minimumValue = Float(min)
slider.maximumValue = Float(max)
slider.value = Float(value)
return slider
}
func updateUIView(_ UI_View: UISlider, context: Context) {
UI_View.value = Float(self.value)
}
func makeCoordinator() -> RedscaleSlider.Coordinator {
Coordinator(value: $value, min: self.min, max: self.max)
}
}
EDIT: Example of how it should be able to be used:
class ViewModel: ObservableObject {
@Published var danger_level: Double
}
struct ExampleView: View {
@ObservedObject var view_model = ViewModel(danger_level: 50)
var body: some View {
VStack {
Text(view_model.danger_level.description)
RedscaleSlider(value: $view_model.danger_level)
// should update view model just like a Stepper would
}
}
}
accentColor
is working fine for me like that..Also your code works fine with a ObservableObject. Here is the demo code: