我有一个定制的SwiftUI选取器.挑选者需要两样东西:小时和分钟.我的 struct 有一个名为MaxDurationSecond的值.我的目标是,如果用户 Select 小时和分钟的组合,这使得总的值超过了MaxDurationSecond,我希望将小时和分钟都重置为零.
var body: some View {
let hours = [Int](0...maxHours)
let minutes = [Int](0...maxMinutes)
GeometryReader { geometry in
HStack(spacing: 0) {
Picker(selection: self.selection.hours, label: Text("")) {
ForEach(0..<maxHours, id: \.self) { index in
Text("\(hours[index]) hr")
.foregroundColor(Color(Asset.Color.V2.white.color))
}
}
.pickerStyle(.wheel)
.frame(width: geometry.size.width / 2, height: geometry.size.height, alignment: .center)
Picker(selection: self.selection.minutes, label: Text("")) {
ForEach(0..<maxMinutes, id: \.self) { index in
Text("\(minutes[index]) min")
.foregroundColor(Color(Asset.Color.V2.white.color))
}
}
.pickerStyle(.wheel)
.frame(width: geometry.size.width / 2, height: geometry.size.height, alignment: .center)
}
}
}
由于文件很大,因此省略了 struct 的其余部分.但以下是必要的信息: 我们正在从 Select 中获得一个绑定,其中有小时和分钟. 最大小时数和最大分钟数分别为24和60.
这就是我正在努力实现的目标:
let hoursInSeconds = selection.hours.wrappedValue
let minutesInSeconds = selection.minutes.wrappedValue
if(hoursInSeconds + minutesInSeconds > Int(maxDurationSeconds)) {
selection.hours.wrappedValue = 0
selection.minutes.wrappedValue = 0
}
我不确定要将此代码包含在何处.我try 对选取器视图执行On Tap手势操作,但它不会更新它们.任何反馈都是非常感谢的.