次のアルゴリズムを使用してストップウォッチを行います。しかし、私が必要としたのは、TimeInterval
クラスを使って特定の時間からカウントダウンするタイマーなので、必要な機能を実装するためにこのコードを少し微調整する必要がありました。どんな助けもありがとう。ここでタイムインターバルクラスを使用するiOSのカウントダウンタイマー
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
@matt私はそれを微調整しようとし、04:45:10のような特定の時間を追加し、そこから00:00:00までカウントダウンを開始しますが、それは正しくできません。 –
"私はそれを微調整し、04:45:10のような特定の時間を追加しようとした。 – matt