以最佳方式处理 UISlider 值

Handle UISlider value in the best way

抱歉这个问题,我需要这里的建议。 我有一个滑块来更改图表中的值(两点之间的距离),我是这样做的,我认为这是最糟糕的方法,但我能找到更好的方法,谁能帮我做得更好,更先进!

我们将不胜感激您的帮助

@objc func stockSliderValueChanged(sender: UISlider) {
    let value = sender.value

    stockChart.lineGap = 0.7

    if value == 0 {
        stockChart.lineGap = stockChart.lineGap - 0.5
    } else if value > 0.0, value <= 0.1 {
        stockChart.lineGap = stockChart.lineGap - 0.45
    } else if value > 0.1, value <= 0.15 {
        stockChart.lineGap = stockChart.lineGap - 0.4
    } else if value > 0.15, value <= 0.2 {
        stockChart.lineGap = stockChart.lineGap - 0.35
    } else if value > 0.2, value <= 0.25 {
        stockChart.lineGap = stockChart.lineGap - 0.3
    } else if value > 0.25, value <= 0.3 {
        stockChart.lineGap = stockChart.lineGap - 0.25
    } else if value > 0.3, value <= 0.35 {
        stockChart.lineGap = stockChart.lineGap - 0.2
    } else if value > 0.35, value <= 0.4 {
        stockChart.lineGap = stockChart.lineGap - 0.15
    } else if value > 0.4, value <= 0.45 {
        stockChart.lineGap = stockChart.lineGap - 0.1
    } else if value > 0.45, value <= 5 {
        stockChart.lineGap = stockChart.lineGap - 0.05
    }

    if value > 0.5, value <= 0.55 {
        stockChart.lineGap = stockChart.lineGap + 0.05
    } else if value > 0.55, value <= 0.6 {
        stockChart.lineGap = stockChart.lineGap + 0.1
    } else if value > 0.6, value <= 0.65 {
        stockChart.lineGap = stockChart.lineGap + 0.15
    } else if value > 0.65, value <= 0.7 {
        stockChart.lineGap = stockChart.lineGap + 0.2
    } else if value > 0.7, value <= 0.75 {
        stockChart.lineGap = stockChart.lineGap + 0.25
    } else if value > 0.75, value <= 0.8 {
        stockChart.lineGap = stockChart.lineGap + 0.3
    } else if value > 0.8, value <= 0.85 {
        stockChart.lineGap = stockChart.lineGap + 0.35
    } else if value > 0.85, value <= 0.9 {
        stockChart.lineGap = stockChart.lineGap + 0.4
    } else if value > 0.9, value <= 0.95 {
        stockChart.lineGap = stockChart.lineGap + 0.45
    } else if value > 0.95, value <= 1 {
        stockChart.lineGap = stockChart.lineGap + 0.5
    }
    stockChart.reDraw()
}

您需要找到描述变换的数学函数。
假设你真的不想要离散的步骤,下面的函数应该可以解决问题(这是未经测试的):

@objc func stockSliderValueChanged(sender: UISlider) {
    let x = sender.value

    let offset = 0.7
    let computedSliderValue = abs(x - 0.5)

    stockChart.lineGap = offset - computedSliderValue
    stockChart.reDraw()
}

如果您确实需要这些步骤,请对 computedSliderValue 应用一些舍入。