Skip to content

daltyboy11/DSColorPicker

Repository files navigation

DSColorPicker

Lightweight views for your color picking needs.

CocoaPods Install

Add pod 'DSColorPicker' to your Podfile and then run $ pod install.

Usage

To use the provided classes, import DSColorPicker.

Presenting the color picker

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()

Presenting

Handling user interaction

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
    }
    
}

User interaction

Reloading a color picker

After updating a color picker's dataSource in some manner, invoke reload(animated:completion:) to communicate the changes to the color picker.

Examples:

Circle Grid

Providing data to the picker

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
    }
}

Providing data to a DSGridColorPickerView

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:

Up to 4 colors per row