My full answer is here. If you don't want to use your own code rather than a third party library, you can do something like the following:
Make your own color picker
Add a UIView
, a UIImageView
, and a UISlider
to the storyboard.
Use this image for the UIImageView:
Set the min and max values for the UISlider
to 0.5 and 13.5.
Hook up the UI elements to the View Controller and use the following code to convert the slider position to colors.
class ViewController: UIViewController {
// RRGGBB hex colors in the same order as the image
let colorArray = [ 0x000000, 0xfe0000, 0xff7900, 0xffb900, 0xffde00, 0xfcff00, 0xd2ff00, 0x05c000, 0x00c0a7, 0x0600ff, 0x6700bf, 0x9500c0, 0xbf0199, 0xffffff ]
@IBOutlet weak var selectedColorView: UIView!
@IBOutlet weak var slider: UISlider!
@IBAction func sliderChanged(sender: AnyObject) {
selectedColorView.backgroundColor = uiColorFromHex(colorArray[Int(slider.value)])
}
func uiColorFromHex(rgbValue: Int) -> UIColor {
let red = CGFloat((rgbValue & 0xFF0000) >> 16) / 0xFF
let green = CGFloat((rgbValue & 0x00FF00) >> 8) / 0xFF
let blue = CGFloat(rgbValue & 0x0000FF) / 0xFF
let alpha = CGFloat(1.0)
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
}
Or by positioning the slider on top of the image and setting the track tints to clear: