昨天我們做到ImageView及TextField會根據Slider的左右滑動而改變顏色及數值,但是如果想要RGB特定值的話,拉Slider那微小的差距會讓人拉到發瘋,所以今天來説説直接利用TextField輸入特定值讓Slider及ImageView來做變化吧
//利用型別的轉換將TextField輸入的字串轉為Int再轉為Float直接設定Slider的值
@IBAction func TextToSlider(_ sender: UITextField){
if let RedStringValue = redValue.text{
if let RedIntValue = Int(RedStringValue){
redSlider.setValue(Float(RedIntValue), animated: true)
}
}
if let GreenStringValue = greenValue.text{
if let GreenIntValue = Int(GreenStringValue){
greenSlider.setValue(Float(GreenIntValue), animated: true)
}
}
if let BlueStringValue = blueValue.text{
if let BlueIntValue = Int(BlueStringValue){
blueSlider.setValue(Float(BlueIntValue), animated: true)
}
}
}
如此一來,簡易調色盤就大功告成啦!