Slider Usage Example

  1. isTrackingIndicates whether the user is dragging Slider, inonEditingChangedBlock to prevent drag and drop from being setTimerModify the
  2. $processValueIn front of$Said binding
  3. inFollowed by the range of sliders
  4. Of the SliderlabelIs not displayed, SwiftUI bug
  5. specifierIt is followed by a string format with two decimal places reserved
  6. Want to invalueDo things when values change, rewriteBindingset, 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…