我知道这不是一个合适的方法,但为了学习的目的,我有一个选择器控件,其中有四个组件,每个组件有 255 行,用于根据 RGB-alpha 更改“ View ”背景颜色。
我知道UIColor(red: <#CGFloat#>, green: <#CGFloat#>, blue: <#CGFloat#>, alpha: <#CGFloat#>)
接受四个 float 作为每种颜色的参数,但出现错误:
找不到接受类型为“(red: Float, green: Float, blue: Float, alpha: Float)”参数列表的“UIColor”类型的初始值设定项
怎么了?
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var newbackgroundColor = UIColor()
var redCode: Float = 0.0
var greenCode: Float = 0.0
var blueCode: Float = 0.0
var alphaCode: Float = 0.0
if component == 0 {
switch row {
case 0..<256:
redCode = Float(row/255)
newbackgroundColor = UIColor(red: redCode, green: greenCode, blue: blueCode, alpha: alphaCode)
default:
break
}
} else if component == 1 {
switch row {
case 0..<256:
greenCode = Float(row/255)
newbackgroundColor = UIColor(red: redCode, green: greenCode, blue: blueCode, alpha: alphaCode)
default:
break
}
} else if component == 2{
switch row {
case 0..<256:
blueCode = Float(row/255)
newbackgroundColor = UIColor(red: redCode, green: greenCode, blue: blueCode, alpha: alphaCode)
default:
break
}
} else {
switch row {
case 0..<256:
alphaCode = Float(row/255)
newbackgroundColor = UIColor(red: redCode, green: greenCode, blue: blueCode, alpha: alphaCode)
default:
break
}
}
self.view.backgroundColor = newbackgroundColor
}
最佳答案
使用CGFloat
而不是Float
当您收到这样的错误时,请检查 API 文档
关于swift - UIColor 初始值设定项(无法找到接受 .... 的类型 'UIColor' 的初始值设定项)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30837531/