UIColor初始化程序(找不到接受....的'UIColor'类型的初始化程序)错误

时间:2015-06-15 04:48:43

标签: swift uipickerview uicolor

我知道这不是一种合适的方法,但为了学习的目的,我有一个带有四个组件的拾取器控件,每个组件有255行来改变基于RGB-alpha的“视图”背景颜色。 我知道UIColor(red: <#CGFloat#>, green: <#CGFloat#>, blue: <#CGFloat#>, alpha: <#CGFloat#>)接受四个浮点数作为每种颜色的参数,但我得到错误:
找不到类型'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
}

2 个答案:

答案 0 :(得分:0)

使用CGFloat而不是Float

当您收到类似

的错误时,请检查API文档

答案 1 :(得分:0)

除了Float与CGFloat不同之外,你可以这样做更容易:

class ViewController: UIViewController,UIPickerViewDelegate {
    @IBOutlet var pickerView: UIPickerView!
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 4
    }
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 256
    }
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        view.backgroundColor = UIColor(red: CGFloat(pickerView.selectedRowInComponent(0))/255, green: CGFloat(pickerView.selectedRowInComponent(1))/255, blue: CGFloat(pickerView.selectedRowInComponent(2))/255, alpha: CGFloat(pickerView.selectedRowInComponent(3))/255)
    }
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return "\(row)"
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        pickerView.delegate = self
        pickerView.backgroundColor = UIColor.whiteColor()
    }
    override func viewDidAppear(animated: Bool) {
        pickerView.selectRow(255, inComponent: 0, animated: false)
        pickerView.selectRow(0, inComponent: 1, animated: false)
        pickerView.selectRow(0, inComponent: 2, animated: false)
        pickerView.selectRow(255, inComponent: 3, animated: false)
        view.backgroundColor = UIColor(red: CGFloat(pickerView.selectedRowInComponent(0))/255, green: CGFloat(pickerView.selectedRowInComponent(1))/255, blue: CGFloat(pickerView.selectedRowInComponent(2))/255, alpha: CGFloat(pickerView.selectedRowInComponent(3))/255)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}