我已经设置了audioPlayer
除了步进器的当前功能之外,我还想为 onIncrement 和 onDecrement 播放单独的声音。
该项目使用 Core Data 进行持久化。$estimatorData.qty 监听已发布的 var 我的视图模型,当数量更改时,新的数量将保存在我的视图模型中 estimatorData.save()
这是Stepper文档的链接
我正在努力思考是否其中一个初始化器适合我想要完成的任务
Stepper("", value: $estimatorData.qty.onChange { qty in
estimatorData.save()
}, in: 0...10000)
.frame(width: 100, height: 35)
.offset(x: -4)
.background(colorScheme == .dark ? Color.blue : Color.blue)
.cornerRadius(8)
Run Code Online (Sandbox Code Playgroud)
这是我的球员
func incrementTaped() {
playSound(sound: "plus", type: "mp3")
}
func decrementTaped() {
playSound(sound: "minus", type: "m4a")
}
Run Code Online (Sandbox Code Playgroud)
目前还没有结合onIncrement/onDecrement函数和value//参数bounds的初始化程序step。
您可以拥有onIncrement和onDecrement:
public init(onIncrement: (() -> Void)?, onDecrement: (() -> Void)?, onEditingChanged: @escaping (Bool) -> Void = { _ in }, @ViewBuilder label: () -> Label)
Run Code Online (Sandbox Code Playgroud)
或value,bounds和step:
public init<V>(value: Binding<V>, in bounds: ClosedRange<V>, step: V.Stride = 1, onEditingChanged: @escaping (Bool) -> Void = { _ in }, @ViewBuilder label: () -> Label) where V : Strideable
Run Code Online (Sandbox Code Playgroud)
相反,您可以创建一个自定义Stepper来组合两个初始化程序:
struct CustomStepper<Label, Value>: View where Label: View, Value: Strideable {
@Binding private var value: Value
private let bounds: ClosedRange<Value>
private let step: Value.Stride
private let onIncrement: (() -> Void)?
private let onDecrement: (() -> Void)?
private let label: () -> Label
@State private var previousValue: Value
public init(
value: Binding<Value>,
in bounds: ClosedRange<Value>,
step: Value.Stride = 1,
onIncrement: (() -> Void)? = nil,
onDecrement: (() -> Void)? = nil,
@ViewBuilder label: @escaping () -> Label
) {
self._value = value
self.bounds = bounds
self.step = step
self.onIncrement = onIncrement
self.onDecrement = onDecrement
self.label = label
self._previousValue = .init(initialValue: value.wrappedValue)
}
var body: some View {
Stepper(
value: $value,
in: bounds,
step: step,
onEditingChanged: onEditingChanged,
label: label
)
}
func onEditingChanged(isEditing: Bool) {
guard !isEditing else {
previousValue = value
return
}
if previousValue < value {
onIncrement?()
} else if previousValue > value {
onDecrement?()
}
}
}
Run Code Online (Sandbox Code Playgroud)
演示
struct ContentView: View {
@State private var value = 1
var body: some View {
CustomStepper(value: $value, in: 1...10, onIncrement: onIncrement, onDecrement: onDecrement) {
Text(String(value))
}
.onChange(of: value) {
print("onChange value: \($0)")
}
}
func onIncrement() {
print("onIncrement")
}
func onDecrement() {
print("onDecrement")
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1640 次 |
| 最近记录: |