2013-04-15 302 views
2

我使用的NSTimer這樣如何暫停/恢復NSTIMER?

GameTimer=[NSTimer scheduledTimerWithTimeInterval:60.0/60.0 target:self selector:@selector(Move) userInfo:nil repeats:YES]; 

在Move方法我更新視圖的位置。

-(void)Move 
{ 

CGRect OldPosition=self.JumbleWordView.frame; 
CGRect NewPosition=CGRectMake(OldPosition.origin.x, OldPosition.origin.y+MovementPerSec, OldPosition.size.width, OldPosition.size.height); 

[self.JumbleWordView setFrame:NewPosition]; 
if (NewPosition.origin.y>=Slot_Left*CurrentBlockSize) 
{ 
    [self checkResult]; 
    [Mytimer invalidate]; 
if (!isAnswerCorrect) 
{ 
    [self swapViews]; 
    TotalRowPenalty=TotalRowPenalty+1; 
    Score=Score+RowIncompletePoints; 
    ScoreLab.text=[NSString stringWithFormat:@"%d",Score]; 
} 
[self LoadNextWord]; 

} 

我該如何去暫停和恢復遊戲計時器?

+0

檢查這一個,不是最好的,但可以幫助你。 [計時器不暫停](http://stackoverflow.com/questions/14101880/nstimer-pause-not-working/14102886#14102886) –

+0

您不能暫停並恢復計時器。您可以使其失效並在您再次需要時創建一個新的。
看看這裏: [here](http://stackoverflow.com/questions/12171990/pause-and-resume-nstimer) – Andrea

+0

有沒有直接的方法來暫停NSTimer,但你可以保存起火日期和當前日期在使定時器無效之前。當你恢復它時,然後創建新的NSTimer對象,並將啓動日期設置爲先前啓用的啓動日期。 你也可以檢查這些答案也是類似的問題。 1. [如何暫停/播放NSTimer?](http://stackoverflow.com/questions/9975562/how-to-pause-play-nstimer)2. [如何暫停和恢復NSTimer在iPhone](http:///stackoverflow.com/questions/4144242/how-to-pause-and-resume-nstimer-in-iphone)希望它可以幫助你。 –

回答

1

有沒有這樣的事情暫停NSTimer,你必須停止並再次啓動它。

[GameTimer invalidate]; //will stop the timer after which you can start it again