好吧,我的第一個應用程序永遠是一個計時器。正如慣例似乎是在我打了個問候世界之後。我使用NSTimer類作爲我的計時器,並且每秒都會觸發它,然後更改UILabel中的文本。現在這一切都很好,除了我第一次運行。每當我在模擬器中運行應用程序並啓動計時器時,它們在我的最後一秒都是一個奇怪的延遲,它會凍結約2秒鐘,然後繼續,如果沒有錯誤。然後它適用於每個運行的集合。NSTimer導致一些奇怪的延遲
這裏就是我成立了的NSTimer代碼:在計時器觸發方法
theTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
然後我打電話updateLabel
countDownLabel.text = [NSString stringWithFormat:@"%02d:%02d", startMin,startSec];
//Check if sets are complete
if(setCount <= numOfSets)
{
setLabel.text = [NSString stringWithFormat:@"Set %02d of %02d", setCount, numOfSets];
//check if timer is complete
if(startMin <= 0 && startSec <= 0)
{
countDownLabel.textColor = [UIColor greenColor];
//run interval timer when timer is complete
if(intervalBetweenSets <= intervalBetweenSetsSetting)
{
setLabel.text = [NSString stringWithFormat:@"Starting set"];
intervalBetweenSets++;
intervalCounterSec--;
countDownLabel.text = [NSString stringWithFormat:@"00:%02d", intervalCounterSec];
}
else
{
//RESET values on timer label
TimerSettings *myTimer = [TimerSettings sharedManager ];
startMin = myTimer.min;
startSec = myTimer.sec;
intervalBetweenSets = 0;
intervalCounterSec = intervalBetweenSetsSetting+1;
countDownLabel.textColor = [UIColor redColor];
countDownLabel.text = [NSString stringWithFormat:@"%02d:%02d", startMin,startSec];
setLabel.text = [NSString stringWithFormat:@"Set %02d of %02d", setCount, numOfSets];
}
}
else
{
//Actual timer for display
if(startSec == 0)
{
startMin = startMin - 1;
}
startSec = startSec - 1;
if(startSec == -1)
{
startSec = 59;
}
if(startMin <= 0 && startSec < 1)
{
//increment number of completed sets
setCount++;
//play sound when timer completes
if (audioPlayer == nil)
;
else
{
[audioPlayer play];
}
}
countDownLabel.text = [NSString stringWithFormat:@"%02d:%02d", startMin,startSec];
}
}
的事情是,我不知道這是否是一些問題與計時器或如果它與我的邏輯有關,如果有什麼不清楚我在做什麼,請提問,我會澄清。
感謝
你是什麼意思的「凍結」?倒計時顯示停止改變?整個用戶界面不會響應觸摸? –
當我說凍結其停止更新的唯一標籤時,應用程序的其餘部分保持響應。我已經通過了它,並且邏輯顯示爲聲音,並且我可以將計時器設置爲任何值,它將在00:01時始終凍結兩秒鐘,然後繼續正常運行 – Armand