0

I've been looking ways to change my thumb image of the slider conditionally for every value change. Since I was not able to find ways to locate the thumb, I used customised slider, using this article. It works perfectly fine, however, I'm not sure how to change the thumb image conditionally. Please help.

Pretty new to SwiftUI.

old_timer
  • 69,149
  • 8
  • 89
  • 168
Ninja
  • 43
  • 9

1 Answers1

0

You can use slider method,

setThumbImage(image, for: .normal)
setThumbImage(image, for: . highlighted)

And as per your requirement if you want to change image on value change you need to add your code like (code is from your attached link sample),

@objc func valueChanged(_ sender: UISlider) {
      self.value.wrappedValue = Double(sender.value)
      let arra: [UIImage?] = [UIImage(systemName: "heart"), UIImage(systemName: "pencil"), UIImage(systemName: "trash")]
      
      sender.setThumbImage(arra.randomElement() as? UIImage, for: .normal)
      sender.setThumbImage(arra.randomElement() as? UIImage, for: .highlighted)
    }
chirag
  • 1,090
  • 9
  • 13
  • Thank you for the answer. That really worked, however, I'm not sure how to change the image on the value change of 1 (Step = 10). It changes so rapidly. Help will be appreciated. – Ninja Oct 12 '20 at 11:36
  • use mySlider.isContinuous = false, This way you will receive valueChanged event only when the user stops moving the slider. – chirag Oct 12 '20 at 12:57
  • more details answer is here, https://stackoverflow.com/questions/9390298/iphone-how-to-detect-the-end-of-slider-drag – chirag Oct 12 '20 at 12:59