我想使用NSTimer产生2秒的延迟如何在程序中初始化定时器?我应该能够使用NSTimer延迟2秒。怎么做?
3
A
回答
12
多在这里选择。
如果你只是想为2秒的延迟,你可以使用睡眠()函数
#include<unistd.h>
...
sleep(2);
或者您可以使用的NSTimer像这样
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(fireMethod) userInfo:nil repeats:NO];
而在你的类,你会有一个方法定义为
-(void)fireMethod
{
//Do stuff
}
+0
似乎你不需要明确地包括unistd 。H。 Xcode 5似乎为你做到了。不知道如何。 – Brenden 2013-12-19 23:32:18
4
在这里你去...
[NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(action)
userInfo:nil
repeats:NO];
-1
请注意,您不应该真的在考虑延迟在事件驱动的用户界面/ 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启动的异步完成例程。
1
简单的答案:[NSThread sleepForTimeInterval:10.0];
相关问题
- 1. 我怎么能给15秒的延迟?
- 2. 1-2秒延迟NStimer倒计时
- 3. 延迟2秒
- 4. Java延迟2秒
- 5. 我怎么能显示3敬酒延迟1秒每个
- 6. swift - 使用NSTimer延迟产卵功能
- 7. 我怎么能延迟静态与PHP
- 8. 我应该怎么做?
- 9. 我该怎么做,我应该
- 10. 为什么phantom.exit()有2秒延迟?
- 11. FTPS 2-5秒延迟
- 12. 我该怎么做才能隐藏按钮5秒钟
- 13. 我应该怎么做才能在java中使用ELement类
- 14. 我该怎么做?
- 15. 我该怎么做?
- 16. 如何延迟几秒钟才能在Imacros中做点什么?
- 17. 我应该怎么做才能优化我的jogl性能?
- 18. 我该怎么做才能拥有
- 19. 我怎么会延迟1秒添加到隐藏的JavaScript功能
- 20. 我应该怎么做来处理WebServiceException:
- 21. C++ 11/Auto - 我应该怎么做?
- 22. (mongo)ID数组:我应该怎么做?
- 23. 我应该怎么做一个聚合?
- 24. 我应该怎么做才能调用audioPlayerDidFinishPlaying:
- 25. 我该怎么做才能让这款Rails应用可测试?
- 26. 我应该怎么做才能完成此功能?
- 27. Ignited-Datatables,应该怎么做?
- 28. netbeans应该怎么做?
- 29. 我该怎么做? nope
- 30. 我该怎么做MongoDB中
可能重复的[iPhone NStimer开始在2秒](http://stackoverflow.com/questions/2784809/iphone-nstimer-start-in-2-seconds) – 2010-09-15 14:17:21