I have a SwiftUI slider configured as follows:
@State var sliderVal:Float = 0.5
@State var sliderDragging = false
And in the body:
Slider(value: $sliderVal) { editing in
if editing {
} else {
}
}
.padding()
.tint(Color.white)
.frame(maxWidth:450)
.onChange(of: sliderVal) { newValue in
//--> applyColorCorrectionOnPhoto with newValue
}
Spacer()
.frame(height:30)
ScrollView(.horizontal) {
HStack(alignment: .lastTextBaseline, content: {
ForEach(supportedColorFilters, id: \.self) { filter in
Spacer(minLength: 10)
ToolButton(label: filter.displayString, image:filter.image, isSelected: false) {
print("Button \(filter) pressed")
editingManager.colorCorrectionControl = filter //new Color filter
let currentValue = fetchCurrentOrDefaultValue()
sliderVal = currentValue
}
Spacer(minLength: 10)
}
})
}
.frame(maxWidth: .infinity)
In a photo editing app where there are color filters such as Brightness, Contrast, Highlights, Shadows, etc., when user chooses Brightness control, the slider should automatically be set to default value of Brightness (without triggering any action to alter brightness in the photo). If the user changes Brightness and then switches to Contrast, the slider should be set to default value (or existing value of contrast if it was applied previously on the photo). This is my intention and what I tried to achieve in the code above.
The code above doesn't always work, not sure if there is anything wrong in the logic. The slider sometimes doesn't get reset to new default value on changing the color control.
Source of truth for slider initial value needs to be in your ViewModel, when the slider view loads, it needs to know the brightness/contrast of the photo. I recommend specifying brightness/contrast as a Published variable in your ViewModel and get a Binding to it in the slider View.