2016-04-01 12 views
0

私はタイマーを設定して、それぞれが完了するとコードを実行します。しかし、NSTimerのタイミングは完全に正確ではないようですが、しばらくするとタイマーは少し早く終了するようです。NSTimerが正確ではありません

私は偏差をテストするために以下を設定しました。

(スタートボタンにストーリーボードに行われ、リンクされている。)

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet weak var startButton: UIButton! 

    //var mainTimer: NSTimer! 
    var counter: NSTimeInterval = 0.0 

    @IBAction func startButtonPressed(sender: AnyObject) { 
    startMainTimer() 
    startTimers() 
    } 

    func startMainTimer() { 
    let mainTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: #selector(ViewController.count), userInfo: nil, repeats: true) 
    NSRunLoop.mainRunLoop().addTimer(mainTimer, forMode: NSRunLoopCommonModes) 
    } 

    func count() { 
    counter += 0.01 
    } 

    func startTimers() { 
    var lastTimeInterval: NSTimeInterval = 0.0 

    for _ in 0..<50 { 
     let timeInterval = lastTimeInterval + 1.0 
     lastTimeInterval = timeInterval 

     // Not setting up a repeating timer as intervals would be different in my original project where the deviation will be even worse. 
     let timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval, target: self, selector: #selector(ViewController.printTime), userInfo: nil, repeats: false) 
     NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes) 
    } 
    } 

    func printTime() { 
    print(counter) 
    } 
} 

しばらくして、タイマーは、第二のまたはそれ以上の初期の第十のであろう。 タイミングを改善するためにNSDateを使用する必要がありますか、それとも複雑すぎるのでしょうか?上記のNSDateの外観はどうですか?

大変助かりました!

+0

NSTimerのドキュメントを見てください:* "タイマーの時間間隔の実効分解能は、50-100ミリ秒のオーダーに制限されています。" * - http://stackoverflow.com参照/ questions/31375361/format-realtime-stopwatch-timer-to-thethth-for-swiftの代わりに –

答えて

1

これは決して完璧ではありませんが、ターゲットセレクタの最後で次のコールをスケジュールすることで、コンパウンドを停止することができます。現在の時刻とトリガーする時刻に基づいて、毎回新しい間隔を計算します。

編集:私はアイデアを与えるために非常に古いプロジェクト(それがobj-Cでだ理由です)からいくつかのコードをつかんで - あなたはこのような何かをしたいではなく、正確に:あなたはこれをコールすると

- (void)constantIntervalTimer { 
    static const NSTimeInterval secondsBetweenMessageSend = 1.0; 

    if (!self.timerShouldStop) { 
     NSDate *startTime = [NSDate date]; 

     // your code here, it should take less than a 
     // second to run or you need to increase the timer interval 

     // figure out the right time to preserve the interval 
     NSTimeInterval waitTime = secondsBetweenMessageSend - 
      [[NSDate date] timeIntervalSinceDate:startTime]; 

     // in case your code runs in more than the interval, 
     // we just call back immediately 
     if (waitTime < 0.0) 
     waitTime = 0.0; 

     [NSTimer scheduledTimerWithTimeInterval: waitTime 
      target:self selector:@selector(constantIntervalTimer) 
      userInfo:nil repeats:NO]; 
    } 
} 

timerShouldStopというプロパティをYESに設定するまで、毎秒それ自身を呼び出し続けます。このプロパティーを宣言して定義し、このメッセージを機能させるには、initでNOに設定する必要があります。

+0

答えをいただきありがとうございます、ルー! NSDateを使用して現在の時刻を取得し、実行時間を設定することを意味しますか?私は現在の時間を常にループでチェックしなければならないと思う。私はNSDateを調べなければなりません。以前はそれを使用したことがありませんでした。私はまだNSTimerをまったく使用しますか? – nontomatic

関連する問題