开发者

How can I update my NSTimer as I change the value of the time interval

开发者 https://www.devze.com 2023-01-21 00:18 出处:网络
I have an NSTtimer implemented and it\'s working fine. I also have the time interval argument connected to a UISlider for the iPhone. However when I change it\'s value the NSTimer is still running at

I have an NSTtimer implemented and it's working fine. I also have the time interval argument connected to a UISlider for the iPhone. However when I change it's value the NSTimer is still running at the original time interval it does not get updated. How can I implement an NSTimer and have it change it's time interval as the value of my UISlider changes. Below is the line I am using for the NSTimer.

[NSTimer scheduledTimerWithTimeInterval:mySlider.value 
                                 target:self 
                               selector:@selector(myMethod) 
                               userIn开发者_开发知识库fo:nil 
                                repeats:YES];

I want it to constantly update it time interval with the value of the UISlider.


You can't I'm afraid. Invalidate it with:

[myTimer invalidate];

Then create a new one with the new time. You may have to set it to nil first as well.

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


You can do something like this using 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];
                   }

               });

And change the value of timeInterval from else where in the code.

0

精彩评论

暂无评论...
验证码 换一张
取 消