4

I create UISlider Programmatically. I try to customise UISlider value as 4.1, 4.2, 4.3, 4.4, 4.5, 5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 6.0, ... Anybody give me the solution

sliderDemo = UISlider(frame:CGRectMake(0, 0, 200,20))
    var numberOfSteps : NSInteger = numbers.count - 1
    sliderDemo.minimumValue = 6.5
    sliderDemo.maximumValue = 4.1
    sliderDemo.continuous = true
    sliderDemo.value = 4.0
    sliderDemo.addTarget(self, action: "sliderValueDidChange:", forControlEvents: .ValueChanged)
    self.view.addSubview(sliderDemo)

func sliderValueDidChange(sender:UISlider!)
{
    println("number:\(sender.value)")
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Bala
  • 1,224
  • 1
  • 13
  • 25

1 Answers1

7

I just create an example of a custom slider for you, in this case I am creating and adding it myself to the form but you can easily adapt to use the one from storyboard, all you need to do is to add your values to the numbers array, the result will be in the variable number in the valueChanged function, you can use observer, notifications or protocol to retrieve the value as it change, or simply call a function from there.

class ViewController: UIViewController {

    var slider:UISlider?
    // These number values represent each slider position
    var numbers = [1, 2, 3, 4, 5, 6, 7] //Add your values here
    var oldIndex = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        slider = UISlider(frame: self.view.bounds)
        self.view.addSubview(slider!)

        // slider values go from 0 to the number of values in your numbers array
        var numberOfSteps = Float(numbers.count - 1)
        slider!.maximumValue = numberOfSteps;
        slider!.minimumValue = 0;

        // As the slider moves it will continously call the -valueChanged:
        slider!.continuous = true; // false makes it call only once you let go
        slider!.addTarget(self, action: "valueChanged:", forControlEvents: .ValueChanged)
    }
    func valueChanged(sender: UISlider) {
        // round the slider position to the nearest index of the numbers array
        var index = (Int)(slider!.value + 0.5);
        slider?.setValue(Float(index), animated: false)
        var number = numbers[index]; // <-- This numeric value you want
        if oldIndex != index{
            println("sliderIndex:\(index)")
            println("number: \(number)")
            oldIndex = index
        }
    }
}

I hope that helps you!

Icaro
  • 14,585
  • 6
  • 60
  • 75
  • 1
    Thanks for reply ... i use ur code its very helpful for me ... i replace the numbers array value by 'var numbers = [4.9, 4.10, 4.12, 5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 5.12, 5.11, 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8, 6.9, 6.12, 6.11]' its partially give output ... The problem is the index is take from 5, so the first value i get is 5.2 ... Coul'd u help me – Bala May 30 '15 at 06:20
  • Sorry I am confuse, I just run with the array you posted and it goes one by one without problem, maybe I am not understanding well what you want to archive. – Icaro May 30 '15 at 06:31
  • I am sure I am able to help you, if I understand what is that you need. Maybe you are changing the minimumValue and maximumValue, if you are don't do that, just change the array and don't read the index but the varible number – Icaro May 30 '15 at 10:26
  • Helped me a lot, thanks, Swift3 fix : `slider!.addTarget(self, action: #selector(valueChanged), for: .valueChanged)` – Dima Gimburg Mar 29 '17 at 20:56
  • Bala, you are getting your first value showing as 5 because you are still setting your minimum at 4.1 in the slider value IB property. Change it to 0 (zero) so that it can read from the first element in the array, which passes the 4.1 value – aremvee Jun 11 '17 at 03:24