Swift 使滑块值仅可单向调节
是否有可能使SwiftUI滑块只向一个方向移动?例如:仅向右侧增加值Swift 使滑块值仅可单向调节,swift,swiftui,Swift,Swiftui,是否有可能使SwiftUI滑块只向一个方向移动?例如:仅向右侧增加值 import SwiftUI struct ContentView: View { @State private var sliderValue = 0.0 var body: some View { VStack { Text(sliderValue.description) Slider(value: $sliderValue)
import SwiftUI
struct ContentView: View {
@State private var sliderValue = 0.0
var body: some View {
VStack {
Text(sliderValue.description)
Slider(value: $sliderValue)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
在我的应用程序中,sliderValue只能调整为增加,不能减少是的,你可以,但你应该使用自定义绑定
import SwiftUI
struct ContentView: View {
@State private var sliderValue = 0.0
var body: some View {
VStack {
Text(sliderValue.description)
Slider(value: Binding.init(get: { () -> Double in return sliderValue },
set: { (newValue) in if (newValue > sliderValue) { sliderValue = newValue } }))
}
}
}