本文介绍了如何在SwiftUI中为Int类型属性创建滑块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的视图具有名为";Score";的Int属性,我要使用滑块调整该属性。
struct IntSlider: View {
@State var score:Int = 0
var body: some View {
VStack{
Text(score.description)
Slider(value: $score, in: 0.0...10.0, step: 1.0)
}
}
}
但SwiftUI的Slider仅适用于双精度/浮点。
如何使其与我的整数一起工作?
推荐答案
struct IntSlider: View {
@State var score: Int = 0
var intProxy: Binding<Double>{
Binding<Double>(get: {
//returns the score as a Double
return Double(score)
}, set: {
//rounds the double to an Int
print($0.description)
score = Int($0)
})
}
var body: some View {
VStack{
Text(score.description)
Slider(value: intProxy , in: 0.0...10.0, step: 1.0, onEditingChanged: {_ in
print(score.description)
})
}
}
}
这篇关于如何在SwiftUI中为Int类型属性创建滑块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!