我用以下代码实现了一个定制的步进器:
struct ContentView: View {
@State private var count: Int? = 0
var body: some View {
HStack {
if let amount = count {
Button {
count = amount - 1
} label: {
Image(systemName: "minus")
.symbolVariant(.circle.fill)
.symbolRenderingMode(.palette)
.foregroundStyle(.background, .tint)
}
Text(amount.formatted())
.monospacedDigit()
.fixedSize()
Button {
count = amount + 1
} label: {
Image(systemName: "plus")
.symbolVariant(.circle.fill)
.symbolRenderingMode(.palette)
.foregroundStyle(.background, .tint)
}
}
}
.buttonRepeatBehavior(.enabled)
}
}
我预计当我按住其中一个按钮时,计数器会反复增加或减少.但是,它只递增或递减一次.也就是说,如果我将count
设置为非可选,并将操作更改为直接修改它,则一切都会按预期进行
在我的应用程序中,我的数据模型比能够重构条件更复杂,那么我如何才能做到这一点呢?
我已经try for each 按钮分配静态id
,但无济于事