是否有适用于iOS的颜色选择器

时间:2015-07-17 01:41:21

标签: ios nscolor

我想让用户能够更改视图的背景颜色。我可以在iOS应用程序中使用内置颜色选择器吗?我一直在搜索和看到对NSColorPanel的引用,但这似乎仅适用于OS X.

3 个答案:

答案 0 :(得分:1)

我认为最简单的解决方案是使用3个UISliders,范围从0到255。这些将代表您的backgroundColor RBG值:

class SliderBGColor: UIViewController {

    var rSlider = UISlider()
    var gSlider = UISlider()
    var bSlider = UISlider()

    override func viewDidLoad() {
        super.viewDidLoad()

        //arbitrary frames for the sliders
        rSlider = UISlider(frame: CGRect(x: 20, y: 100, width: self.view.frame.width - 40, height: 44))
        gSlider = UISlider(frame: CGRect(x: 20, y: 200, width: self.view.frame.width - 40, height: 44))
        bSlider = UISlider(frame: CGRect(x: 20, y: 300, width: self.view.frame.width - 40, height: 44))

        rSlider.minimumValue = 0
        gSlider.minimumValue = 0
        bSlider.minimumValue = 0

        rSlider.maximumValue = 255
        gSlider.maximumValue = 255
        bSlider.maximumValue = 255

        //background starts off white
        rSlider.value = 255
        gSlider.value = 255
        bSlider.value = 255

        rSlider.addTarget(self, action: "updateBackgroundColor", forControlEvents: .ValueChanged)
        gSlider.addTarget(self, action: "updateBackgroundColor", forControlEvents: .ValueChanged)
        bSlider.addTarget(self, action: "updateBackgroundColor", forControlEvents: .ValueChanged)

    self.view.addSubview(rSlider)
    self.view.addSubview(gSlider)
    self.view.addSubview(bSlider)

}

func updateBackgroundColor() {

    // Calculate the RGB values
    let red = rSlider.value/255.0
    let green = gSlider.value/255.0
    let blue = bSlider.value/255.0

    // set the background color
    self.view.backgroundColor = UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: 1)

}

}

答案 1 :(得分:0)

您没有这样的内置组件,但您可以使用https://github.com/jjxtra/DRColorPicker之类的库来执行此操作。

请查看此主题以获取更详尽的列表: Is there a color picker library/code for iPhone development?

答案 2 :(得分:0)

不,iOS中没有内置颜色选择器。但是,有很多开源颜色选择器控件。 (查看http://www.cocoacontrols.com