【问题标题】:Creating a Countdown Watch with a slider in Xcode在 Xcode 中创建带有滑块的倒计时手表
【发布时间】:2024-05-20 03:40:01
【问题描述】:

大家好,我需要一些帮助。

我正在尝试创建一个可以从小时、分钟和秒倒计时的倒计时手表。现在我只能创建几秒钟的倒计时,但我希望应用程序能够在我用滑块“滑动”时更新秒、分和小时。我发现很难正确更新标签并将“小时和分钟”添加到应用程序中。有人可以帮我弄清楚逻辑。

这是我到目前为止编写的代码,它只在几秒钟内就可以正常工作。我还添加了一个音频文件,正如你在代码中看到的那样,它会在最后播放。

class ViewController: UIViewController {

var secondsCount = 30;
var timer = Timer()
var audioPlayer = AVAudioPlayer()

@IBOutlet weak var label: UILabel!
@IBOutlet weak var labelmin: UILabel!


    // Slideren som slider tid for sal 1
@IBOutlet weak var sliderOutlet: UISlider!
@IBAction func slider(_ sender: UISlider)
{
    //Live changes the numbers
    secondsCount = Int(sender.value)
    label.text = String(secondsCount) + " Seconds"

}


    //Start button
@IBOutlet weak var startOutlet: UIButton!
@IBAction func start(_ sender: Any)
{
        //Nederstående kode aktiverer funktionen counter()
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.counter), userInfo: nil, repeats: true)

    sliderOutlet.isHidden = true
    startOutlet.isHidden = true
}

    //Counter function
func counter() {
    secondsCount -= 1

    label.text = String(secondsCount) + " Seconds"

    if (secondsCount == 0)
    {
        timer.invalidate()

        audioPlayer.play()
    }
}

    //Stop button
@IBOutlet weak var stopOutlet: UIButton!
@IBAction func stop(_ sender: Any)
{
    timer.invalidate()
    secondsCount = 30
    sliderOutlet.setValue(30, animated: true)
    label.text = "30 Seconds"

    audioPlayer.stop()

    sliderOutlet.isHidden = false
    startOutlet.isHidden = false
}


    // viewDidLoad

override func viewDidLoad()
{
    super.viewDidLoad()

    do
    {
        let audioPath = Bundle.main.path(forResource: "1", ofType: ".mp3")

        try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!))

    }
    catch
    {
        //ERROR
    }



}

【问题讨论】:

  • 滑块的最小值和最大值是多少?起始值是多少?而且,当滑块值发生变化时,您希望发生什么?
  • 也许更好的问题:您希望以什么格式显示“倒计时”? “02:34:21”?还是“2 小时 34 分 21 秒”?还是别的什么?
  • 我希望它采用“2 小时 34 分钟 21 秒”格式,倒计时手表只需从 2 小时开始倒计时......而不是更多......那是因为它是为了一个特定的目的。
  • 您希望滑块的分辨率是多少?如果将滑块移动 1 个像素会使起始值改变 1 秒,那么您的滑块需要 7200 像素长才能具有 0-2 小时的范围。

标签: ios iphone swift xcode cocoa-touch


【解决方案1】:

这是将秒数转换为格式化的小时、分钟、秒字符串的一种方法:

func hmsFromSecondsFormatted(seconds: Int) -> String {

    let h = seconds / 3600
    let m = (seconds % 3600) / 60
    let s = seconds % 60

    var newText = ""

    if h > 0 {
        newText += "\(h)"
        if h == 1 {
            newText += " hour, "
        } else {
            newText += " hours, "
        }
    }

    if m > 0 || h > 0 {
        newText += "\(m)"
        if m == 1 {
            newText += " minute, "
        } else {
            newText += " minutes, "
        }
    }

    newText += "\(s)"
    if s == 1 {
        newText += " second"
    } else {
        newText += " seconds"
    }

    return newText

}

那么你可以这样使用它:

label.text = hmsFromSecondsFormatted(secondsCount)

多个if 条件给你两件事:

  1. 单数/复数时间组件名称的结果(因此您得到“1 秒”而不是“1 秒”),并且

  2. 仅返回必要的时间分量。因此,45 秒返回为“45 秒”而不是“0 小时、0 分钟、45 秒”

在您的实际应用中,您可能还会使用本地化字符串作为时间组件名称。

希望有帮助:)

【讨论】:

  • 这正是我需要的那种逻辑,非常感谢您的意见!
最近更新 更多