如何启动和停止NSTimer?

时间:2022-12-29 20:46:07

I develop Stop Watch Application. In my application, there are Two UIButtons , StartBtn and StopBtn, And also I use NSTimer.

我开发了秒表应用程序。在我的应用程序中,有两个UIButton,StartBtn和StopBtn,我也使用NSTimer。

Now, i want to start NSTimer when user click on StartBtn and also stop when your click on StopBtn.

现在,我想在用户点击StartBtn时启动NSTimer,并在点击StopBtn时停止。

I know that NSTimer is stopped by [MyTimerName invalidate]; method but I don't know how to start NSTimer again?

我知道NSTimer被[MyTimerName invalidate]停止了;方法,但我不知道如何再次启动NSTimer?

3 个解决方案

#1


90  

The NSTimer class is a bit awkward to use; rather than separating the creation/destruction from the start/stop, it's all rolled together. In other words the timer starts as soon as it's created and stops as soon as it's destroyed.

NSTimer类使用起来有点尴尬;它不是将创建/破坏与开始/停止分开,而是将它们全部组合在一起。换句话说,计时器一旦创建就会立即启动,并在计时器被销毁后立即停止。

You therefore need to use the existence of the NSTimer object as a flag to indicate if it's running; something like this:

因此,您需要使用NSTimer对象的存在作为标志来指示它是否正在运行;像这样的东西:

// Private Methods
@interface MyClass ()
{
    NSTimer *_timer;
}
- (void)_timerFired:(NSTimer *)timer;
@end

@implementation MyClass

- (IBAction)startTimer:(id)sender {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                  target:self
                                                selector:@selector(_timerFired:)
                                                userInfo:nil
                                                 repeats:YES];
    }
}

- (IBAction)stopTimer:(id)sender {
    if ([_timer isValid]) {
        [_timer invalidate];
    }
    _timer = nil;
}

- (void)_timerFired:(NSTimer *)timer {
    NSLog(@"ping");
}

#2


14  

You could always use fire to start a NStimer again

您可以随时使用fire来重新启动NStimer

  [timer fire];

To stop it:

要阻止它:

  [timer invalidate];
  //remember to set timer to nil after calling invalidate;
  timer = nil;

#3


8  

You can start the timer through

你可以通过启动计时器

#define kRefreshTimeInSeconds 1
 NSTimer *myTimerName;
 .
 .
 myTimerName = [NSTimer scheduledTimerWithTimeInterval: kRefreshTimeInSeconds 
                                   target:self 
                                   selector:@selector(handleTimer:) 
                                   userInfo:nil
                                   repeats:YES];

Then the delegate function:

然后是委托函数:

-(void)handleTimer: (id) sender 
{       
   //Update Values in Label here
}

And to stop Timer

并停止计时器

-(void)stopTimer: (id) sender 
{       
   if(myTimerName)
   {
       [myTimerName invalidate];
        myTimerName = nil;
   }
}

#1


90  

The NSTimer class is a bit awkward to use; rather than separating the creation/destruction from the start/stop, it's all rolled together. In other words the timer starts as soon as it's created and stops as soon as it's destroyed.

NSTimer类使用起来有点尴尬;它不是将创建/破坏与开始/停止分开,而是将它们全部组合在一起。换句话说,计时器一旦创建就会立即启动,并在计时器被销毁后立即停止。

You therefore need to use the existence of the NSTimer object as a flag to indicate if it's running; something like this:

因此,您需要使用NSTimer对象的存在作为标志来指示它是否正在运行;像这样的东西:

// Private Methods
@interface MyClass ()
{
    NSTimer *_timer;
}
- (void)_timerFired:(NSTimer *)timer;
@end

@implementation MyClass

- (IBAction)startTimer:(id)sender {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                  target:self
                                                selector:@selector(_timerFired:)
                                                userInfo:nil
                                                 repeats:YES];
    }
}

- (IBAction)stopTimer:(id)sender {
    if ([_timer isValid]) {
        [_timer invalidate];
    }
    _timer = nil;
}

- (void)_timerFired:(NSTimer *)timer {
    NSLog(@"ping");
}

#2


14  

You could always use fire to start a NStimer again

您可以随时使用fire来重新启动NStimer

  [timer fire];

To stop it:

要阻止它:

  [timer invalidate];
  //remember to set timer to nil after calling invalidate;
  timer = nil;

#3


8  

You can start the timer through

你可以通过启动计时器

#define kRefreshTimeInSeconds 1
 NSTimer *myTimerName;
 .
 .
 myTimerName = [NSTimer scheduledTimerWithTimeInterval: kRefreshTimeInSeconds 
                                   target:self 
                                   selector:@selector(handleTimer:) 
                                   userInfo:nil
                                   repeats:YES];

Then the delegate function:

然后是委托函数:

-(void)handleTimer: (id) sender 
{       
   //Update Values in Label here
}

And to stop Timer

并停止计时器

-(void)stopTimer: (id) sender 
{       
   if(myTimerName)
   {
       [myTimerName invalidate];
        myTimerName = nil;
   }
}