【发布时间】: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 代码是没有意义的;问题在于你的真实代码。