CC's After Hours project on how to create custom sliders and use slider values for Core Image filters.
- Fork and Clone this repo.
- Open the
CustomSlider.xcodeproj
starter file. - Review the storyboard scenes and view controller files.
- Create outlets for the slider and
valueLabel
- Create an action for the sliderValue and make it a class of
CustomSlider
- Create a class at the end of this file and call it
CustomSlider
. - Make the
CustomSlider
of typeUISlider
and insert this function:
override func trackRect(forBounds bounds: CGRect) -> CGRect {
let point = CGPoint(x: bounds.minX, y: bounds.midY)
return CGRect(origin: point, size: CGSize(width: bounds.width, height: 20)) // height changes the height of the slider
}
- Update the text in the
valueLabel
to show the value change from the slider. - Default the
valueLabel
to say "0% satisfied" - Programmatically changed the min and max track tint color, and set the thumb image on the slider.
- Create 2 properties to save your 2 colors.
- Create outlets for your
swatchSquare
imageView, sliders, and 2 color buttons. - Create actions for red, green, and blue values changed.
- Add
updateViews
function inside each action.
- Add
- Create
updateViews
function.- Set each red, green, and blue value to a CGFloat value.
- Set the
swatchSquare
's background color.
- Create actions for
saveColor
buttons.- Set each color to current color.
- Implement
prepare for segue
method.- Set
destinationVC
colors to properties at the top of this file.
- Set
- Call
updateViews
function inside ofviewDidLoad
- Create properties to hold color properties from the segue.
- Create properties for Core Image context and filter.
- Create an outlet for an
imageView
. - Creat an action for the "Check Me Out" button.
- Set the context to a new instance of
CIContext
. - Unwrap the color properties from the passed segue.
- Set the filter to a new instance of
CIFilter
with the CIFilter's name, set the parameters to the color properties, and specify the origin and width. - Set the output image to the current
imageView
's image.
- Set the context to a new instance of
- Try the slider and see the different colors on the left (min track) and right (max track).
- Notice the label updates the text when the value of the slider changes.
- Try the sliders, notice how "mixing" the different sliders (red, green, and blue) creates a new swatch color.
- Create a new swatch color, and save it as Color 1. Create a different swatch color and save it as Color 2.
- Click on the "Check Me Out" button.
- A checkboard with your saved Color 1 and Color 2 colors should appear.
- Go back and create a new swatch color, resave, and try the "Check Me Out" button again. Pretty neat filter, huh? This Core Image filter uses a CIVector object to "draw" the checkboard pattern with the 2 input colors. More information here.
Please visit this repo.