Slider Usage Example
isTracking
Indicates whether the user is dragging Slider, inonEditingChanged
Block to prevent drag and drop from being setTimer
Modify the$processValue
In front of$
Said bindingin
Followed by the range of sliders- Of the Slider
label
Is not displayed, SwiftUI bug specifier
It is followed by a string format with two decimal places reserved- Want to in
value
Do things when values change, rewriteBinding
的set
, the callsliderChanged
// // ContentView.swift // SliderDemo // // Created by dacaiguoguo on 2021/7/5. // import SwiftUI var isTracking:Bool = false struct ContentView: View { @State var processValue:CGFloat = 0 var body: some View { VStack { Slider(value: $processValue, in: 0 ... 100, label: { Text("aValue") }) Slider(value: $processValue, in: 0 ... 100) { Label( title: { Text("Label") }, icon: { Image(systemName: "star.fill") } ) } Slider(value: $processValue, in: 0... 100, step: 1, onEditingChanged: { isEditing in debugPrint("isEditing: \(isEditing)") isTracking = isEditing }, minimumValueLabel: Text("0"), maximumValueLabel: Text("100")) { Text("Hello, world!" ) } Text("Hello \(processValue, specifier: "%.2f")") .padding() Slider(value: Binding(get: { processValue }, set: { (newVal) in processValue = newVal sliderChanged() }), in: 0 ... 100, step: 1, onEditingChanged: { isEditing in debugPrint("isEditing: \(isEditing)") isTracking = isEditing }) }.onAppear() { Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { atimer in if ! isTracking { processValue += 1 if processValue > 100 { processValue = 0 } } } } } func sliderChanged() { print("Slider value changed to \(processValue)") } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }Copy the code
Developer.apple.com/tutorials/s…