0

I have a UISlider inside a cell and I set its alpha to 1 but when the slider is displayed the alpha is stuck at 0.5. How do I change it?

enter image description here

lazy var videoSlider: UISlider = {
    let slider = UISlider()
    slider.translatesAutoresizingMaskIntoConstraints = false
    slider.minimumTrackTintColor = UIColor.orange
    slider.maximumTrackTintColor = UIColor.white
    slider.setThumbImage(UIImage(named: "sliderThumb"), for: .normal)
    slider.isEnabled = false
    slider.alpha = 1.0 // set at 1 here and never changed
    return slider
}()

I also tried:

override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.black

    videoSlider.alpha = 0
    UIView.animate(withDuration: 0.33, animations: {
        self.videoSlider.alpha = 1
    })
}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256

1 Answers1

1

It was the period time observer that was causing the problem. As the time progressed and the track moved along for some reason the slider color was getting reset to 0.5. To fix it I updated the alpha on the main queue.

timeObserverToken = player?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using: { [weak self](progressTime) in

    let seconds: Float64 = CMTimeGetSeconds(progressTime)

    if let duration = self?.player?.currentItem?.duration {

        let durationSeconds: Float64 = CMTimeGetSeconds(duration)

        // even though this is already happening on the main queue it didn't work without it
        DispatchQueue.main.async {
            self?.videoSlider.alpha = 1
        }

        self.videoSlider.value = Float(seconds / durationSeconds)
    }
})
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256