我在模型MyModel中有一个变量myValue。在这个例子中,我希望myValue永远只是4个值中的一个(现实生活中的50个值,以防它影响答案)。假设我希望myValue是此数组中的一个值:let myArray = [4, 2, 7, 5]
我希望滑块能够改变myValue。但是,滑块只接受Double作为类型。
我正在尝试解决这个问题,但到目前为止,我只想出了一个有点复杂的解决方案……
// TYPE
enum MyType: Int, CaseIterable, Codable {
case zero = 4
case one = 2
case two = 7
case three = 5
var asIndex : Double {
switch self {
case .zero: return 0
case .one: return 1
case .two: return 2
case .three: return 3
}
}
}
// MODEL
struct MyModel: Codable {
var myValue: MyType = .zero
}
// VIEWS
struct MyView: View {
@Binding var myModel: MyModel
var body: some View {
Slider(value: Binding.init(
get: { () -> Double in return myModel.myValue.asIndex },
set: { newValue in
if let unwrappedMyType = MyType(rawValue: Int(newValue)) { myModel = MyModel(myValue: unwrappedMyType) } }
), in: 0...3)
Text("\(myModel.myValue.rawValue)" as String) // 4, 2, 7 or 5 depending on the slider being set to values 0,1,2 or 3
}
}
struct ContentView: View {
@State var myModel: MyModel = MyModel()
var body: some View {
MyView(myModel: $myModel)
.frame(width:300, height: 100)
}
}它几乎可以工作了,但是滑块不能被设置。我做错了什么?有没有更简单或者更好的方法来做到这一点呢?
发布于 2021-03-26 13:41:04
默认情况下,Slider在范围内是连续的,所以映射到枚举的重要部分是给它一个步骤。
以下是可能的解决方案。使用Xcode12.4/ iOS 14.4进行测试

以下仅为修改后的零件:
enum MyType: Int, CaseIterable, Codable {
case zero = 4
case one = 2
case two = 7
case three = 5
init(index: Double = 0) {
if let type = Self.allCases.first(where: { $0.asIndex == index }) {
self = type
} else {
self = .zero
}
}
var asIndex : Double {
switch self {
case .zero: return 0
case .one: return 1
case .two: return 2
case .three: return 3
}
}
}
// VIEWS
struct MyView: View {
@Binding var myModel: MyModel
var body: some View {
Slider(value: Binding(
get: { myModel.myValue.asIndex },
set: { myModel.myValue = MyType(index: $0) }
), in: 0...3, step: 1) // give step !!
Text("\(myModel.myValue.rawValue)" as String) // 4, 2, 7 or 5 depending on the slider being set to values 0,1,2 or 3
}
}https://stackoverflow.com/questions/66811292
复制相似问题