我应该能够使用 NSTimer 延迟 2 秒。怎么做呢?

发布于 2024-09-19 04:38:53 字数 43 浏览 2 评论 0原文

我想使用 NSTimer 产生 2 秒的延迟,如何在程序中初始化计时器?

I want to produce a delay of 2 seconds using NSTimer how to initialize timer in program?

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

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

发布评论

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

评论(4

梦里的微风 2024-09-26 04:38:53

这里有多个选项。

如果你只想要 2 秒的延迟,你可以使用 sleep() 函数

#include<unistd.h>
...
sleep(2);

或者你可以像这样使用 NSTimer

[NSTimer    scheduledTimerWithTimeInterval:2.0    target:self    selector:@selector(fireMethod)    userInfo:nil repeats:NO];

在你的类中你将有一个定义为的方法

-(void)fireMethod
{
//Do stuff 
}

Multiple options here.

If you just want a delay of 2 seconds you could use the sleep() function

#include<unistd.h>
...
sleep(2);

Or you may be able to use NSTimer like so

[NSTimer    scheduledTimerWithTimeInterval:2.0    target:self    selector:@selector(fireMethod)    userInfo:nil repeats:NO];

And in your class you would have a method defined as

-(void)fireMethod
{
//Do stuff 
}
等风来 2024-09-26 04:38:53

干得好...

[NSTimer scheduledTimerWithTimeInterval:2 
                                     target:self 
                                   selector:@selector(action) 
                                   userInfo:nil 
                                    repeats:NO];

Here you go...

[NSTimer scheduledTimerWithTimeInterval:2 
                                     target:self 
                                   selector:@selector(action) 
                                   userInfo:nil 
                                    repeats:NO];
执妄 2024-09-26 04:38:53

简单答案:[NSThread sleepForTimeInterval:10.0];

Simple answer: [NSThread sleepForTimeInterval:10.0];

谈情不如逗狗 2024-09-26 04:38:53

请注意,您不应该真正考虑事件驱动的 UI/OS 中的延迟。您应该考虑现在要执行的任务以及以后要执行的任务,并对这些子任务进行编码并适当地安排它们。例如,

// code that will block the UI when done in the main thread
- (void) methodC {
  doA();
  delay(2);
  doB();
}

您可能希望代码看起来更像:

- (void) methodA {
  doA();
  return;  // back to the run loop where other useful stuff might happen
}

- (void) methodB {
  doB();
}

然后您可以在 methodA 末尾使用 NSTimer 调度 methodB,NSTimer 由任何调用的 methodA 启动,或者最好的选择是通过异步完成例程由 methodA 开始的事情。

Note that you should not really be thinking about delays in an event driven UI/OS. You should thinking about tasks you want to do now, and tasks you want to do later, and code these subtasks and schedule them appropriately. e.g. instead of:

// code that will block the UI when done in the main thread
- (void) methodC {
  doA();
  delay(2);
  doB();
}

you might want to have code that looks more like:

- (void) methodA {
  doA();
  return;  // back to the run loop where other useful stuff might happen
}

- (void) methodB {
  doB();
}

and you can then schedule methodB with an NSTimer at the end of methodA, an NSTimer started by whatever called methodA, or, the best option, by the asynchronous completion routine of something started by methodA.

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