我想使用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. 我怎麼能顯示3敬酒延遲1秒每個
- 2. swift - 使用NSTimer延遲產卵功能
- 3. 我怎麼能延遲靜態與PHP
- 4. 我應該怎麼做?
- 5. 我該怎麼做才能隱藏按鈕5秒鐘
- 6. 我該怎麼做?
- 7. 我應該怎麼做才能優化我的jogl性能?
- 8. 我怎麼會延遲1秒添加到隱藏的JavaScript功能
- 9. 我應該怎麼做一個聚合?
- 10. (mongo)ID數組:我應該怎麼做?
- 11. 我應該怎麼做才能調用audioPlayerDidFinishPlaying:
- 12. 我應該怎麼做才能完成此功能?
- 13. netbeans應該怎麼做?
- 14. Ignited-Datatables,應該怎麼做?
- 15. 我該怎麼做MongoDB中
- 16. 我該怎麼做PyQt4?
- 17. regexp freebie ...我該怎麼做?
- 18. 反思?我該怎麼做?
- 19. 我該怎麼做(遊戲)?
- 20. 如何添加延遲2或3秒
- 21. jquery在2秒後顯示延遲
- 22. 延遲不到一秒鐘,Swift 2
- 23. 我該如何延遲功能
- 24. 我怎樣才能使用延遲()與show()和隱藏()在Jquery
- 25. 我應該怎麼做GWT使用<th>而不是?
- 26. 使用套接字的C#代理,我應該怎麼做?
- 27. 對於使用stimulsoft的繁重報告,我應該怎麼做?
- 28. 我應該怎麼做才能解決NameError在軌道上
- 29. 我應該怎麼做才能讓窗口被激活?
- 30. Vbscript延遲5秒
可能重複的[iPhone NStimer開始在2秒](http://stackoverflow.com/questions/2784809/iphone-nstimer-start-in-2-seconds) – 2010-09-15 14:17:21