UISlider 使步骤可见

UISlider to get steps visible

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

我在 Google 上搜索过,但一无所获。

故事板

StepSliderViewController Class

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.
    }
    */

}

结果