2016-02-19 143 views
0

我對Xcode(swift)很陌生,簡而言之,我試圖製作一個倒數計時器,倒計時秒數和小數點。用毫秒創建倒數計時器(NSTimer),但它不能正確倒計數

它倒計時,因爲它應該,但是,例如,當我從13秒倒數,它真的需要23秒,當我手動計時。 這可能是一個簡單的解決方案,但我已經嘗試了很多東西,無法弄清楚如何解決它。

這裏是我的代碼:

import UIKit 

var timer = NSTimer() 

var seconds = 13 

var milliseconds = 0 

class ViewController: UIViewController { 

    @IBOutlet weak var Label: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     Label.text = "\(seconds).\(milliseconds)" 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func Button(sender: AnyObject) { 

     timer = NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: Selector("countDownfunction"), userInfo: nil, repeats: true) 

    } 


    func countDownfunction() { 

     Label.text = "\(seconds).\(milliseconds)" 

     if milliseconds == 0 { 

      seconds -= 1 
      milliseconds = 100 
     } else { 
      milliseconds -= 1 
     } 

    } 

} 

編輯:新的代碼還沒有計算出來,但是,正確的方式我想,我怎樣才能。減去/像我一樣在比較兩個字符串:標籤。 text =(countdownSeconds - elapsedTime) 它給了我一個錯誤,我不能' - '兩個字符串,我不知道如何比較兩秒鐘。謝謝

import UIKit 

class ViewController: UIViewController { 


@IBOutlet weak var Label: UILabel! 

var startTime = NSDate.timeIntervalSinceReferenceDate() 

var currentTime = NSDate.timeIntervalSinceReferenceDate() 

var elapsedTime = String() 

var differenceTime = String() 

var countdownSeconds = String() 


override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 



@IBAction func Button(sender: AnyObject) { 
    countdownSeconds = String(10) 

    startTime = NSDate.timeIntervalSinceReferenceDate() 

    NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("countDownUpdateMethod"), userInfo: nil, repeats: true) 



} 

func countDownUpdateMethod(){ 

    currentTime = NSDate.timeIntervalSinceReferenceDate() 

    elapsedTime = String(currentTime - startTime) 



    Label.text = (countdownSeconds - elapsedTime) 



} 



} 
+0

'NSTimer'分辨率不夠高 – dan

回答

1

你忽略了它其實需要來安排和執行調用countDownfunction的時間。 NSTimer不會在恰好100毫秒內回撥您,因爲手機正在做其他事情。這根本不會那麼準確。此外,countDownfunction電話本身將需要時間,你沒有考慮到這一點。

您應該通過記錄開始時間和檢查countDownfunction中的當前時間,然後減去兩者來設置您的標籤來解決此問題。這樣可以彌補處理定時器的延遲。

您可以使用NSDate.timeIntervalSinceReferenceDate()獲取當前時間。

+0

啊好吧,謝謝! – Josh

+0

@Josh如果您滿意,請接受答案,謝謝。 –

+0

是的,我會的,我忘了,但我還沒有完全弄清楚。 var Seconds從13開始倒計時,我將如何做,然後將其與NSDate.timeIntervalSinceReferenceDate()進行比較以確保其正確時間? – Josh

0

您是否注意到您將毫秒設置爲100,而不是1000?

所以你的計時器在23秒內被調用約1300次。這聽起來是正確的。是什麼讓你覺得每秒倒計數1000次是個好主意?人們看不到。

如果您閱讀NSTimer的文檔,重複計時器將嘗試在預期時間觸發(因此它們在最後一次觸發後不會觸發X秒,但會在計時器後觸發X,2X,3X和4X秒)被啓動),但是如果在給定時間內沒有觸發定時器,那麼該觸發器將被丟棄。因此,在23秒內,您的計時器不會觸發23,000次,但會達到預期的1300次。您無法每秒更新屏幕1000次。如果可以的話,沒有人能夠看到它。

以合理的速度運行您的計時器,每秒不超過30次,並計算出時間。不是基於倒計時,而是基於實際時間。

+0

是的ganesher你是對的,我沒有注意到這是100毫秒我的錯誤。但從我聽說NSTimer不是很準確,所以我不想通過使用NSDate.timeIntervalSinceReferenceDate()來了解它更準確。我確實更新了新代碼,你有時間查看嗎?謝謝。 @ gnasher729 – Josh