UISlider使步骤可见

时间:2019-04-24 05:27:47

标签: swift uislider

我想自定义UISlider以显示步骤。我知道这种滑块本身就存在,因为Apple在“设置”>“常规”>“辅助功能”>“文本大小”中使用了它。

我在Google上搜索,但什么也没找到。

1 个答案:

答案 0 :(得分:0)

StoryBoard

enter image description here

StepSliderViewController类

import UIKit

class StepSliderViewController: UIViewController {

    @IBOutlet weak var slder: UISlider!
    @IBOutlet weak var twoLeadSp: NSLayoutConstraint!
    @IBOutlet weak var threeLeadSp: NSLayoutConstraint!
    @IBOutlet weak var fiveLeadSp: NSLayoutConstraint!
    @IBOutlet weak var sixLeadSp: NSLayoutConstraint!


    var lastStep: Float = 0
    var stepValue: Float = 15

    override func viewDidLoad() {
        super.viewDidLoad()

        twoLeadSp.constant = -(slder.layer.frame.size.width / 6)
        threeLeadSp.constant = -((slder.layer.frame.size.width / 6) * 2)
        fiveLeadSp.constant = -((slder.layer.frame.size.width / 6) * 4)
        sixLeadSp.constant = -((slder.layer.frame.size.width / 6) * 5)

        self.slder.value = 45.0
        self.lastStep = (self.slder.value) / self.stepValue;
    }

    @IBAction func sliderChange(_ sender: Any) {
        let newStep = round((slder.value) / self.stepValue)
        self.slder.value = newStep * self.stepValue
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

结果

enter image description here

相关问题