Make blink effect to the thumb in UISlider – Swift

Rather than setting the alpha of the whole slider, just use a more “faded” version of the thumb image.

First, you need to get a faded version of thumb:

let thumb = UIImage(...)! // create the thumb image as you did before
UIGraphicsBeginImageContextWithOptions(thumb.size, false, thumb.scale)
thumb.draw(at: .zero, blendMode: .normal, alpha: 0.5) // find an alpha value that you like
let fadedThumb = UIGraphicsGetImageFromCurrentImageContext()

You’d probably want to put thumb and fadedThumb at the class level, so you don’t recreate them every time the timer is triggered. Also, add a isUsingFadedThumb property to track the current state:

var isUsingFadedThumb = false

And then just:

Timer.scheduledTimer(withTimeInterval: 0.8,repeats: true)
    {_ in 
        if self.isUsingFadedThumb {
            self.slider.setThumbImage(thumb, for: .normal)
        } else {
            self.slider.setThumbImage(fadedThumb, for: .normal)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top