在Xcode中使用滑块创建倒计时手表 [英] Creating a Countdown Watch with a slider in Xcode

查看:80
本文介绍了在Xcode中使用滑块创建倒计时手表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello guys i need some help.

我正在尝试创建一个倒计时手表,该手表可以从小时,分钟和秒开始倒数。现在,我只能创建数秒的倒计时,但是当我使用滑块滑动时,我希望应用程序能够更新秒,分钟和小时。我发现很难正确更新标签并向应用程序添加小时和分钟。有人可以帮我弄清楚逻辑。

Im trying to create a countdown watch which can count down from hours, minuts and seconds. Right now im only able to create the countdown for seconds but i want the application to be able to update the seconds, minutes and hours when i "slide" with the slider. I find it very hard to update the label correctly and add "hours and minutes" to the application. Can someone please help me figuring the logic out.

这是我到目前为止编写的代码,仅在几秒钟内即可正常工作。.我还添加了一个音频文件,最终可以播放该文件代码。

This is the code i have written so far and it works fine only with seconds.. I also added a audio file which will play in the end as you can see in the code.

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
    }



}


推荐答案

这里是一个将秒数转换为格式化的小时,分​​钟,秒字符串的方法:

Here is one way to convert number of seconds into a formatted hours, minutes, seconds string:

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

}

然后您可以像这样使用它:

Then you could use it like this:

label.text = hmsFromSecondsFormatted(secondsCount)

倍数 if 条件给您两件事:


  1. 单/复数时间分量名称的结果(因此您将获得 1秒而不是 1秒),并且

  1. a result of singular / plural time component names (so you get "1 second" instead of "1 seconds"), and

仅返回必要的时间分量。因此,将45秒返回为 45秒,而不是 0小时0分45秒

returns only the necessary time components. So, 45 seconds is returned as "45 seconds" instead of "0 hours, 0 minutes, 45 seconds"

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

In your actual app, you probably will also use localized strings for the time component names.

希望有帮助:)

这篇关于在Xcode中使用滑块创建倒计时手表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆