How To Make a Custom Control Tutorial: A Reusable Slider |

Controls are the bread and butter of iOS apps. UIKit provides many but this tutorial shows you how to make an iOS custom control in Swift.

This is a companion discussion topic for the original entry at

Really cool! I like it.

1 Like

Excellent. Great content. Emphasises how little I know about Core Animation though :]

Really glad you like it! Cheers! :]

Hello and thank you for the tutorial.

I wonder the following, maybe you can help:

What if in that custom control, I would want to add the following additional behavior: if I touchDown on one of the two thumbsliders, I want to trigger some action.

How do I build that trigger?

Writing “sendActions(for: .touchDown)” into the “beginTracking” function does not seem to be the solution. What is the solution?

Independently from this question, and starting from your final App, if I add the line “sendActions(for: .valueChanged)” into the beginTracking function (and I do not change anything else), then when I click somewhere in the control, it triggers the action. I do not drag, just click. Why is that? What value changed that would have caused a .valueChanged event? Thank you for your thoughts!

@leamars Can you please help with this when you get a chance? Thank you - much appreciated! :]


When you add sendActions(for: .valueChanged) into the beginTracking function, it will begin get triggered when a touch happens, regardless whether it’s a drag or just a touch down. Apple Developer Documentation

For just touching down on either of the two thumbnails you could do something really simple like just add a .touchUpInside target to the rangeSlider in the ViewController like so:

rangeSlider.addTarget(self, action: #selector(touchThumbnail(_:)), for: .touchUpInside)

And the appropriate function to call like this:
@objc func touchThumbnail(_ rangeSlider: RangeSlider) { print("A thumbnail was clicked!") }

Then to know which one was clicked you could add a property to the rangeSlider that would track that.

Thank you very much, this helps!

1 Like

Hello, how can I put a gradient on the selected track?

@leamars Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hey! I recommend you check out this tutorial on glossy buttons that covers the topic more in depth., in particular the “Adding a Gradient” section.

But, here’s a snippet you can use by adding it to the RangeSliderTrackLayer:


let colorSpace = CGColorSpaceCreateDeviceRGB()

// 2
let colorLocations: [CGFloat] = [0.0, 1.0]

// 3
let startColor =
let endColor =
let colors: CFArray = [startColor, endColor] as CFArray

// 4
guard let gradient = CGGradient(
    colorsSpace: colorSpace, colors: colors, locations: colorLocations) else {

let startPoint = CGPoint(x: rect.midX, y: rect.minY)
let endPoint = CGPoint(x: rect.midX, y: rect.maxY)

ctx.drawLinearGradient(gradient, start: startPoint, end: endPoint, options: [])


hello how do i change the position of the slider

@leamars Do you have any feedback about this? Thank you - much appreciated! :]

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!