编辑
完整代码https://forums.raywenderlich.com/t/swiftui-getting-started-raywenderlich-com/73445/11?u=prashantkt
我正在SwiftUI中创建一个演示,以匹配https://www.raywenderlich.com/3715234-swiftui-getting-started的颜色
我已经对该演示进行了一些实验,如果您需要更多代码或信息,请告诉我:)
这是第一次运行时的样子
点击“匹配我”按钮时,我会计算分数
显示警报并保持滑块值
Button(action: {
self.score = self.computeScore()
self.resetTheColor()
self.needToShoWAlert = true
}) {
Text("Match me")
}
func resetTheColor () {
rTarget = Double.random(in: 0..<1)
gTarget = Double.random(in: 0..<1)
bTarget = Double.random(in: 0..<1)
sliderBinder.reset()
}
这是sliderBinder类
class SliderBindable:BindableObject {
var r:Double = 0 {
didSet {
didChange.send((r,g,b))
}
}
var g:Double = 0 {
didSet {
didChange.send((r,g,b))
}
}
var b:Double = 0 {
didSet {
didChange.send((r,g,b))
}
}
var didChange = PassthroughSubject<(r:Double,g:Double,b:Double),Never>()
func reset() {
r = 0
g = 0
b = 0
}
}
r,g,b值静止时标签缩小
查看屏幕截图