iphone NStimer 在 2 秒内启动
我试图让一段代码在“启动”后 2 秒运行。
我认为 NSTimer 可以做到这一点,但无法弄清楚。
I am trying to have a block of code run 2 seconds after I 'start' it.
I think the NSTimer can do this but can't figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可以使用 NSTimer,但另一种选择是使用 performSelector:withObject:afterDelay: 基本上就像稍后发生的方法调用(消息发送)。
此示例将在延迟后发送一条 doStuff: 消息:
这会导致该方法在 2.0 秒后被调用:
NSTimer can be used, but another option is to use performSelector:withObject:afterDelay: It's basically like a method call (message send) that happens later.
This example will send a doStuff: message after a delay:
which causes this method to get invoked 2.0 seconds later:
以下将满足您的需要:
然后是委托函数:
The following will do what you need:
Then the delegate function:
您还可以使用一些方便的代码:
NSObject+PerformBlock.h
NSObject+PerformBlock.m
然后只需导入 NSObject+PerformBlock.h 并调用:
You can also use some handy code :
NSObject+PerformBlock.h
NSObject+PerformBlock.m
Then just import NSObject+PerformBlock.h and call :
您应该能够将 NSTimeInterval 设置为 2.0 秒,并且它应该在该时间后触发。你看到什么了?您用来调用计时器的代码是什么?
You should be able to set the
NSTimeInterval
to 2.0 seconds and it should fire after that amount of time. What are you seeing? What is the code you are using to invoke the timer?