0

I have a UISlider and I am trying to make the position go when the user taps to a certain time, instead of moving the thumb.

I tried to work it through this topic and this answer and I came to this approach. This is what I tried:

var slider: UISlider! // and maxValue, etc added in viewDidLoad

func sliderTapped(gestureRecognizer: UIGestureRecognizer) {
    var pointTapped: CGPoint = gestureRecognizer.locationInView(self.view)

    var positionOfSlider: CGPoint = slider.frame.origin
    var widthOfSlider: CGFloat = slider.frame.size.width
    var newValue = ((pointTapped.x - positionOfSlider.x) * CGFloat(slider.maximumValue) / widthOfSlider)

    slider.setValue(Float(newValue), animated: true)

}

But, it is not letting me anywhere on slider and get the tapped value. It only lets me hold the thumb and slide it, but not tapping.

Community
  • 1
  • 1
senty
  • 12,385
  • 28
  • 130
  • 260

2 Answers2

1

Well the error is legit. If you check the operands for setValue you'll see several possible calling sequences. The one you probably want expects a Float (as opposed to a CGFloat) - but also requires an animation: boolean flag.

Try something like:

let floatNewValue = Float(newValue)
durationSlider.setValue(floatNewValue,animated: true)
jbbenni
  • 1,158
  • 12
  • 31
  • Yes, this solved the problem. I have updated the question just now. It's not helping me achieve what I want to achieve – senty Jan 05 '16 at 17:11
0

UISlider's method is func setValue(_ value: Float, animated animated: Bool).

You'll need to cast your CGFloat to a Float.

So durationSlider.setValue(Float(newValue), animated: true)

aahrens
  • 5,522
  • 7
  • 39
  • 63
  • It removed the error but doesn't change the slider's value on tapping. It only moves the slider if I hold the thumb and slide. Do you have any idea? Edit: Just updated the question – senty Jan 05 '16 at 17:11