UISlider可以看到步骤

问题描述 投票:0回答:1

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

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

swift uislider
1个回答
0
投票

故事板

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

© www.soinside.com 2019 - 2024. All rights reserved.