Lightweight views for your color picking needs.
Add pod 'DSColorPicker'
to your Podfile and then run $ pod install
.
To use the provided classes, import DSColorPicker
.
After creating either a Grid or Circle DSColorPickerView, invoke show(animated:completion:)
. By default, the presentation is animated and the completion handler does nothing.
Example:
In a view controller
let gridColorPickerView = DSGridColorPickerView(frame: self.view.bounds, delegate: self, dataSource: self)
view.addSubview(gridColorPickerView)
gridColorPickerView.show()
Receive a notification when the user selects a color in the picker by adopting the DSColorPickerViewDelegate
protocol.
Example:
extension ViewController: DSColorPickerViewDelegate {
// A UIViewController subclass is the delegate for a picker view, and changes its view's background color when a color is selected by the user in a picker view.
func didSelect(color: UIColor, pickerView: DSColorPickerViewType) {
view.backgroundColor = color
}
}
After updating a color picker's dataSource
in some manner, invoke reload(animated:completion:)
to communicate the changes to the color picker.
Examples:
Adopt the DSColorPickerViewDataSource
to provide the number of colors and color values to the picker.
Example of a conforming view controller that provides various RGB formatted colors to the picker:
extension ViewController: DSColorPickerViewDataSource {
var numberOfColors: Int {
return 16
}
func color(at index: Int) -> CGColor {
let color = UIColor(hue: CGFloat(index) / CGFloat(self.numberOfColors), saturation: 1.0, brightness: 1.0, alpha: 1.0).cgColor
return color
}
}
To provide data to a DSGridColorPickerView
, adopt the DSGridColorPickerViewDataSource
protocol. This data source has the same requirements as the DSColorPickerViewDataSource
, with the addition of maxColumns
, which specifies the maximum allowable number of columns in the grid.
Example - Adding colors one by one to the picker with maxColumns
set to 4
: