2013-08-27 17 views
1

以下是我在touchesMoved:withEvent:中使用的代碼。 定時器將用下面的代碼零星火:NSTimer射擊儘管失效並將其設置爲零

if(_pauseTimer) { 
       [_pauseTimer invalidate]; 
       _pauseTimer=nil; 
      } 
      //Restart the pause timer. 
      _pauseTimer = [NSTimer scheduledTimerWithTimeInterval:_settings.pauseTime 
                   target:self 
                  selector:@selector(timerExpired:) 
                   userInfo:_pauseTimer 
                  repeats:NO]; 

然而,一旦予註釋掉_pauseTimer =零,它正確地表現;即,除非調用touchesMoved:withEvent:之間的時間爲_settings.pauseTime,否則它不會再次被觸發。爲什麼這個「老」_pauseTimer儘管失效並且設置爲零,

+7

爲什麼你傳遞'_pauseTimer'作爲'userInfo'參數?這會導致計時器保留自己。您應該在那裏傳遞'nil'。 – jlehr

+1

這不是一個答案,但你有沒有嘗試過這樣做呢..http://stackoverflow.com/questions/15383782/how-to-reset-nstimer-for-touch-touch-move-in-ios – Krishnan

+0

Invalidate應該停止它...設置一個斷點 –

回答

0

我看到您的問題的2個可能的原因。

  1. userInfo 定時器的用戶信息。您指定的對象由定時器保留,並在定時器失效時釋放。該參數可能爲零。

  2. 您必須從安裝了定時器的線程發送無效消息。如果您從另一個線程發送此消息,則與定時器關聯的輸入源可能不會從其運行循環中刪除,這可能會阻止線程正常退出。