【发布时间】:2021-12-30 22:58:33
【问题描述】:
如果没有来自我没有的第二台设备的录音很难解释,但是当我尝试滑动滑块时,当我的手指肯定还在移动时它会停止。
我在下面发布了我的代码。我很乐意回答任何问题并进行解释。我确信这是我应该知道的非常简单的事情。任何帮助将不胜感激,谢谢!
import SwiftUI
class SettingsViewModel: ObservableObject {
@Published var selectedTips = [
10.0,
15.0,
18.0,
20.0,
25.0
]
func addTip() {
selectedTips.append(0.0)
selectedTips.sort()
}
func removeTip(index: Int) {
selectedTips.remove(at: index)
selectedTips = selectedTips.compactMap{ $0 }
}
}
struct SettingsTipsView: View {
@StateObject var model = SettingsViewModel()
var body: some View {
List {
HStack {
Text("Edit Suggested Tips")
.font(.title2)
.fontWeight(.semibold)
Spacer()
if(model.selectedTips.count < 5) {
Button(action: { model.addTip() }, label: {
Image(systemName: "plus.circle.fill")
.renderingMode(.original)
.font(.title3)
.padding(.horizontal, 10)
})
.buttonStyle(BorderlessButtonStyle())
}
}
ForEach(model.selectedTips, id: \.self) { tip in
let i = model.selectedTips.firstIndex(of: tip)!
//If I don't have this debug line here then the LAST slider in the list tries to force the value to 1 constantly, even if I remove the last one, the new last slider does the same. It's from a separate file but it's pretty much the same as the array above. An explanation would be great.
Text("\(CalculatorViewModel.suggestedTips[i])")
HStack {
Text("\(tip, specifier: "%.0f")%")
Slider(value: $model.selectedTips[i], in: 1...99, label: { Text("Label") })
if(model.selectedTips.count > 1) {
Button(action: { model.removeTip(index: i) }, label: {
Image(systemName: "minus.circle.fill")
.renderingMode(.original)
.font(.title3)
.padding(.horizontal, 10)
})
.buttonStyle(BorderlessButtonStyle())
}
}
}
}
}
}
【问题讨论】:
-
看起来你可能需要在
ForEach中使用 iOS15 的基于列表的绑定 - 在 hackingwithswift.com/quick-start/swiftui/… 使用 Swift 的技巧进行黑客攻击应该会给你一些线索吗? -
我实际上也允许 iOS 14 上的设备使用该应用程序,您还有什么可以推荐的吗?