【问题标题】:A count down timer for iOS using Time Interval Class使用时间间隔类的 iOS 倒数计时器
【发布时间】:2017-04-23 23:42:09
【问题描述】:

我正在使用以下算法来制作秒表。但我需要的是一个计时器,可以使用TimeInterval 类从特定的给定时间倒计时,所以我需要稍微调整一下这段代码,以便能够实现我需要的功能。任何帮助将不胜感激。

Here is the link of the tutorial.

@IBAction func start(sender: AnyObject) {
    let aSelector : Selector = “updateTime”
    timeTimer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(ViewController.updateTime), userInfo: nil, repeats: true)

    startTime = Date.timeIntervalSinceReferenceDate
}   

var startTime = TimeInterval()
var timeTimer = Timer()

func updateTime() {
    //let currentTime = date.parseDuration(timeString: "\(displayTimeLabel.text)")
    let currentTime = Date.timeIntervalSinceReferenceDate

    //Find the difference between current time and start time.
    var elapsedTime: TimeInterval = currentTime - startTime

    //calculate the minutes in elapsed time.
    let minutes = UInt8(elapsedTime / 60.0)

    elapsedTime -= (TimeInterval(minutes) * 60)

    //calculate the seconds in elapsed time.
    let seconds = UInt8(elapsedTime)

    elapsedTime -= TimeInterval(seconds)

    //find out the fraction of milliseconds to be displayed.
    let fraction = UInt8(elapsedTime * 100)

    //add the leading zero for minutes, seconds and millseconds and store them as string constants

    let strMinutes = String(format: "%02d", minutes)
    let strSeconds = String(format: "%02d", seconds)
    let strFraction = String(format: "%02d", fraction)

    //concatenate minuets, seconds and milliseconds as assign it to the UILabel
    displayTimeLabel.text = "\(strMinutes):\(strSeconds):\(strFraction)"
}

这是我尝试做的:

我使用扩展将String解析为TimeInterval

    extension Date {

    func parseDuration(timeString:String) -> TimeInterval {

        guard !timeString.isEmpty else {
            return 0
        }

        var interval:Double = 0

        let parts = timeString.components(separatedBy:":")
        for (index, part) in parts.reversed().enumerated() {
            interval += (Double(part) ?? 0) * pow(Double(60), Double(index))
        }

        return interval
    }
}

然后添加我需要的开始和结束时间。但是输出不正确。我每次都从显示屏上获取当前时间。

let constTimeString = "15:04:46"

var date = Date()

var startTime = TimeInterval()
var timeTimer = Timer()

    func updateTimeAction() {

        timeTimer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(ViewController.updateTime2), userInfo: nil, repeats: true)
        startTime = date.parseDuration(timeString: constTimeString)

    }

func updateTime2() {

         let currentTime = date.parseDuration(timeString: displayTimeLabel.text!)
        var elapsedTime: TimeInterval = currentTime - startTime
        print("elapsed: \(elapsedTime)")


        //calculate the minutes in elapsed time.

        let minutes = Int(elapsedTime / 60.0)
        print("minutes: \(minutes)")
        elapsedTime -= (TimeInterval(minutes) * 60)

        //calculate the seconds in elapsed time.

        let seconds = Int(elapsedTime)
        print("seconds: \(seconds)")
        elapsedTime -= TimeInterval(seconds)

        //find out the fraction of milliseconds to be displayed.

        let fraction = Int(elapsedTime * 100)
        print("fraction \(fraction)")
        //add the leading zero for minutes, seconds and millseconds and store them as string constants

        let strMinutes = String(format: "%02d", minutes)
        let strSeconds = String(format: "%02d", seconds)
        let strFraction = String(format: "%02d", fraction)

        //concatenate minuets, seconds and milliseconds as assign it to the UILabel

        displayTimeLabel.text = "\(strMinutes):\(strSeconds):\(strFraction)"
    }

【问题讨论】:

  • 还有什么问题?
  • @matt 我试图调整它并添加一个特定的时间,例如 04:45:10,然后从那里开始倒计时到 00:00:00,但无法正确。
  • "我试图调整它并添加一个特定的时间,例如 04:45:10" 我在您的代码中的任何地方都没有看到。
  • @matt 好的,我会说的。我没有,因为它没用。
  • 这不是没用的:这是问题。你需要展示在做什么并解释出了什么问题。显示 other 代码是没有意义的;问题在于你的真实代码

标签: ios swift timer stopwatch


【解决方案1】:

你的错误在这里:

let currentTime = date.parseDuration(timeString: displayTimeLabel.text!)
var elapsedTime: TimeInterval = currentTime - startTime

永远不要获取displayTimeLabel.text 的值。它是view,而不是model。你唯一应该做的就是设置它。跟踪幕后的模型(您正在操作的数据)。

好的,所以回到以前的方式。记录开始计数时的开始时间,以当前时间作为当前时间,减去即可得知已经过去了多少时间:

let currentTime = Date.timeIntervalSinceReferenceDate
var elapsedTime: TimeInterval = currentTime - startTime

好的,现在您知道要从 constTimeString 中减去多少经过的时间(解析为时间间隔)来了解现在要在 displayTimeLabel.text 中显示的时间字符串。

【讨论】:

  • 好的...谢谢,我会更新我的代码并将结果回复给您。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
相关资源
最近更新 更多