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

发布于 2024-09-28 11:33:52 字数 570 浏览 0 评论 0原文

我已经实现了 NSTtimer 并且工作正常。我还将时间间隔参数连接到 iPhoneUISlider。但是,当我更改它的值时,NSTimer 仍然以原始时间间隔运行,它不会更新。如何实现 NSTimer 并让它随着我的 UISlider 值的变化而改变它的时间间隔。下面是我用于 NSTimer 的行。

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

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

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) 
                               userInfo:nil 
                                repeats:YES];

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪是无色的血 2024-10-05 11:33:52

恐怕你不能。使其无效:

[myTimer invalidate];

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

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

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];
朮生 2024-10-05 11:33:52

您可以使用 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 的值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文