Setting Slider Value through UIPanGestureRecognizer

Hi, I've been trying to set the slider value in the same direction with finger drag movement on the screen. From right to left, decrease the slider value and vice versa. I have set up the slider outlet and the UIPanGestureRecognizer. I'm having trouble with assigning the slider thumbRect on "addPanGesture(slider: UISlider)" method when I call it. If I assign the slider, the whole slider starts moving. I'm aiming to move the thumbRect in accordance with the UIPanGestureRecognizer. I'm sharing the relevant code. In case, you need the whole code, I'd be more than happy to share it with you. Any suggestion please ?


class PhotoViewController: UIViewController {

   @IBOutlet weak var effectsSlider: UISlider!

   var initialCenter: CGPoint!

override func viewDidLoad() {
    super.viewDidLoad()

     initialCenter = effectsSlider.frame.origin
}

func addPanGesture(slider: UISlider) {
    let pan = UIPanGestureRecognizer(target: self, action: #selector(PhotoViewController.handlePan(sender:)))
    view.addGestureRecognizer(pan)
  }
 
  @objc func handlePan(sender: UIPanGestureRecognizer) {
   
    let panView = sender.view!
    let translation = sender.translation(in: view)
   
    switch sender.state {
    case .began, .changed:
      panView.center = CGPoint(x: panView.center.x + translation.x, y: panView.center.y + translation.y)
      sender.setTranslation(CGPoint.zero, in: view)
     
    case .ended:
      return
    default:
      break
    }
  }
}

Replies

I don't understand what you're trying to achieve.


Tracking thumb is buit in the UISlider. What do you want to change ?


Of course, you can design a complete slider (I created circular, or sliders without thumb, …) by subclassing UIControl. But is it what you need ?

Hi Claude, thank you for your response.


I actually don't even want to have a tracking thumb.


What I'm trying to achieve is I want the slider to move in accordance with a finger swipe movement.


Let's say when the finger moves from right to left in anywhere within the view, I would like the slider's value(the blue color filling the range) to move towards the negative end. And for vice versa, the slider's value should be moving towards the positive range.


To achieve this I think I need to be able to assign the "effectsSlider" outlet on the "addPanGesture" method. When I do that the whole slider is moving instead of what I'm trying to achieve. I think I need an extra variable for that.