2016-12-08 4 views
-2

次のアルゴリズムを使用してストップウォッチを行います。しかし、私が必要としたのは、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)" 
    } 
+0

質問は何ですか? – matt

+0

@matt私はそれを微調整しようとし、04:45:10のような特定の時間を追加し、そこから00:00:00までカウントダウンを開始しますが、それは正しくできません。 –

+0

"私はそれを微調整し、04:45:10のような特定の時間を追加しようとした。 – matt

答えて

0

あなたの間違いはここにある:

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

はこれまで、全くdisplayTimeLabel.textの値を取得しないでください。 ビューではなく、モデルです。あなたがそれと一緒にすべき唯一のことは、それが設定されています。舞台裏でモデル(操作しているデータ)を追跡します。

さて、以前のように戻ってください。あなたがカウントを開始し、現在の時刻と現在の時刻を使用して、経過時間を学ぶために減算開始時刻を記録:

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

大丈夫、今は(constTimeStringから減算する時間を経過どのくらい知っています時間間隔に解析して)、今すぐ表示する時刻文字列をdisplayTimeLabel.textに学習します。

+0

Okk ...ありがとう私は私のコードを更新し、結果をあなたに戻ってきます。ありがとう。 –

関連する問題