【问题标题】:Swift: Snapping UISlider to nearest valueSwift:将 UISlider 捕捉到最接近的值
【发布时间】:2015-06-03 05:37:02
【问题描述】:

我为练习创建了一个简单的小费计算器,并为“小费​​百分比”和“拆分”金额实现了UISliders

拆分金额将总账单金额除以拆分UISlider 的值显然,但我遇到的问题是它正在不断更新滑块......我没有选中“持续更新”来自属性检查器,甚至将uislider.continuousupdates = false 添加到代码中,但没有运气。

如何让UISlider 捕捉到最接近的整数值?

感谢您提供任何帮助。

【问题讨论】:

  • 我创建了一个 custom slide 并在 stackoverflow 上发布另一个问题,看看它可能是你要找的。​​span>

标签: swift uislider


【解决方案1】:

您始终可以在其 IBOutlet 函数中将滑块值转换为 Int ,然后从那里调用任何其他方法,或者如果您正在尝试将滑块的值更改为该 Int 。不确定这是否是您要找的,但是:

let constantValue = 8 // Don't change this

@IBAction func slider(_ sender: UISlider) // Connect your UISlider to this
{
    var sliderValue = Int(sender.value) // Get the nearest Int value from your slider
    // Just a quick print statement to make sure we're getting the right number from the slider
    print("\(constantValue)==D, Slider's nearest whole number: \(sliderValue)")
    sender.value = sliderValue // This will set the slider's value to the nearest Int
    exampleFunction(sliderValue) // Optionally call another example function from here using the new whole number from the slider
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 2021-12-17
    相关资源
    最近更新 更多