How can I programmatically pause an NSTimer?

前端 未结 15 1787
梦毁少年i
梦毁少年i 2020-11-27 13:06

I\'m using an NSTimer to do some rendering in an OpenGL based iPhone app. I have a modal dialog box that pops up and requests user input. While the user is providing input

15条回答
  •  佛祖请我去吃肉
    2020-11-27 13:25

    You cant pause NSTimer as mentioned above. So the time to be captured should not be dependent on Timer firedate ,i suggest. Here is my simplest solution :

    When creating the timer initialize the starting unit time like:

    self.startDate=[NSDate date];
    self.timeElapsedInterval=[[NSDate date] timeIntervalSinceDate:self.startDate];//This ``will be 0 //second at the start of the timer.``     
    
    myTimer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self `selector:@selector(updateTimer) userInfo:nil repeats:YES];
    

    ` Now in the update timer method:

      NSTimeInterval unitTime=1;
    -(void) updateTimer
     {
     if(self.timerPaused)
           {
               //Do nothing as timer is paused
           }
     else{
         self.timerElapsedInterval=timerElapsedInterval+unitInterval;
        //Then do your thing update the visual timer texfield or something.
        }
    
     }
    

提交回复
热议问题