UISlider with increments of 5

后端 未结 6 1173
离开以前
离开以前 2020-11-29 01:16

How do I have my UISlider go from 1-100 in increments of 5?

6条回答
  •  臣服心动
    2020-11-29 01:35

    For anyone looking for the Swift syntax...

    override func viewDidLoad() {
        super.viewDidLoad()
        mySlider.addTarget(self, action: "sliderValueDidChange:", forControlEvents: .ValueChanged)
    }
    
    func sliderValueDidChange(sender:UISlider!)
    {
        //Set value to the nearest 5...
        print((Float)((Int)((sender.value + 2.5) / 5) * 5))
        sender.setValue((Float)((Int)((sender.value + 2.5) / 5) * 5), animated: false)
    }
    

    Alternatively, just create an Action function with the Assistant Editor (what I did ultimately)...

    @IBAction func mySliderValueChanged(sender: UISlider) {
        //Set value to the nearest int
        sender.setValue(Float(roundf(sender.value)), animated: false)
    
    }
    

提交回复
热议问题