当我更改时间间隔的值时,如何更新我的NSTimer

我有一个NSTtimer实施,它工作正常。 我也有连接到iPhoneUISlider的时间间隔参数。 但是,当我改变它的值时, NSTimer仍然在原来的时间间隔运行,它不会被更新。 我怎样才能实现一个NSTimer ,并改变它的时间间隔,因为我的UISlider的值发生了变化。 以下是我用于NSTimer

 [NSTimer scheduledTimerWithTimeInterval:mySlider.value target:self selector:@selector(myMethod) userInfo:nil repeats:YES]; 

我希望它不断地用UISlider的值更新它的时间间隔。

你不能害怕。 使用以下方法使其无效:

 [myTimer invalidate]; 

然后用新的时间创build一个新的。 您可能必须先将其设置为零。

 myTimer = nil; myTimer = [NSTimer scheduledTimerWithTimeInterval:mySlider.value target:self selector:@selector(myMethod) userInfo:nil repeats:YES]; 

你可以用GCD做这样的事情

 NSTimeInterval timeInterval=1; BOOL timerInvalidate=NO; dispatch_async(dispatch_get_global_queue(0, 0), ^{ while (!timerInvalidate){ dispatch_async(dispatch_get_global_queue(0, 0), ^{ //your code; }); [NSThread sleepForTimeInterval:timeInterval]; } }); 

并从代码中的其他位置更改timeInterval的值。