ColorSlider

public class ColorSlider: UIControl

ColorSlider is a customizable color picker with live preview.

Inspired by Snapchat, ColorSlider lets you drag to select black, white, or any color in between. Customize ColorSlider and its preview via a simple API, and receive callbacks via UIControlEvents.

Use the convenience initializer to create a .vertical ColorSlider with a live preview that appears to the .left of it:

let colorSlider = ColorSlider(orientation: .vertical, previewSide: .left)

You can create a custom preview view using the ColorSliderPreviewing protocol, or by subclassing DefaultPreviewView. To pass in a custom preview view, simply use the default initializer instead:

let myPreviewView = MyPreviewView()
let colorSlider = ColorSlider(orientation: .vertical, previewView: myPreviewView)

ColorSlider is a UIControl subclass and fully supports the following UIControlEvents:

  • .valueChanged
  • .touchDown
  • .touchUpInside
  • .touchUpOutside
  • .touchCancel

Once adding your class as a target, you can get callbacks via the color property:

colorSlider.addTarget(self, action: #selector(ViewController.changedColor(_:)), forControlEvents: .valueChanged)

func changedColor(_ slider: ColorSlider) {
    var color = slider.color
    // ...
}

Customize the appearance of ColorSlider by setting properties on the gradientView:

// Add a border
colorSlider.gradientView.layer.borderWidth = 2.0
colorSlider.gradientView.layer.borderColor = UIColor.white

// Disable rounded corners
colorSlider.gradientView.automaticallyAdjustsCornerRadius = false

ColorSlider uses the HSB color standard internally. You can set the saturation of your ColorSlider’s gradientView to change the saturation of colors on the slider. See the GradientView and HSBColor for more details on how colors are calculated.

  • The selected color.

    Declaration

    Swift

    public var color: UIColor
  • The background gradient view.

    Declaration

    Swift

    public let gradientView: GradientView
  • The preview view, passed in the required initializer.

    Declaration

    Swift

    public let previewView: PreviewView?
  • Declaration

    Swift

    required public init(orientation: Orientation, previewView: PreviewView?)

    Parameters

    orientation

    The orientation of the ColorSlider.

    previewView

    An optional preview view that stays anchored to the slider. See ColorSliderPreviewing.