首页 > 解决方案 > 停止时如何使滑块处于当前位置

问题描述

我正在制作计时器应用程序,并且我有倒计时滑块。我想在暂停时获取滑块的当前位置。

我尝试使用Int(value.sender),但出现以下错误消息:

sliderOutlet.setValue(seconds, animated: true)

无法使用类型为“(Int,动画:Bool)”的参数列表调用“setValue”

@IBOutlet weak var sliderOutlet: UISlider!

@IBAction func slider(_ sender: UISlider)
{
    seconds = Int(sender.value)
    timeScreen.text = timeString(time: TimeInterval(seconds))
} 

@IBAction func startButton(_ sender: UIButton)
{
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(CountdownViewController.counter), userInfo: nil, repeats: true)

    sliderOutlet.isHidden = true
}

@IBAction func pauseButton(_ sender: UIButton) {
    stop()
    timeScreen.text = timeString(time: TimeInterval(seconds))
    sliderOutlet.setValue(seconds, animated: true)
}

func stop()
{
    timer.invalidate()
    sliderOutlet.isHidden = false
}

我预计当我点击 pauseButton 时,sliderOutlet 将显示当前秒的位置。

标签: iosswiftuislider

解决方案


setValue(_:animated:)方法需要一个Float,而不是一个Int

sliderOutlet.setValue(Float(seconds), animated: true)

确保 的值seconds介于您为滑块设置的最小值和最大值之间,默认值为0.01.0


推荐阅读