如何以非阻塞延迟执行多个操作?
我需要将第一张图像放在屏幕上,然后在一秒后放置第二张图像,然后再过一秒后放置最后一张图像。另外,这应该以非阻塞方式完成:)在这种情况下如何执行?正在考虑嵌套多个 UIView animateWithDuration 类方法,但我不需要任何动画,我只需要以直接方式将多个图像放在屏幕上,并在每个步骤后延迟。
更新:恕我直言,使用 NSTimer 来解决这个简单的问题太重量级了。我应该需要获得一个参考,安排 3 个不同的计时器(或者一个非常 if'y 方法的计时器),然后以某种方式停止。如果是这样,我想用一种方法来完成这一切。在这种情况下,如果我使用 ScheduledTimerWithTimeInterval 启动计时器,那么恕我直言,我需要在其他地方停止(无效)?
I need to put first image on screen, then put the second after 1 second, and then - the last one after one more second. Also, this should be done in non-blocking manner :) How do you perform that in such cases? Was thinking about nesting several UIView animateWithDuration class methods, but I do not need any animations, I just need to put several images on screen in straight manner with a delay after each step.
UPDATE: IMHO it's too heavyweight approach to use NSTimer for this simple problem. I should need to get a reference, schedule 3 different timers (or one with very if'y method) and then to stop somehow. If so, I would like to to it all from within one method. In that case, if I initiate timer with scheduledTimerWithTimeInterval then IMHO I need to stop (invalidate) somewhere else or smth?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 NSObject 的
performSelector:withObject:afterDelay:
。Use
NSObject
'sperformSelector:withObject:afterDelay:
.使用[NSThread detachNewThread:Selector:toTarget:withObject:]。您可以使用
[[NSThread currentThread] isFinished]
获取当前线程完成时的事件。这就是您可以以非阻塞方式完成所有任务的方式。有关详细信息,请参阅 NSThread此处的类参考快乐编码!
use
[NSThread detachNewThread:Selector:toTarget:withObject:]
. You can get the event when the current thread is finished by using[[NSThread currentThread] isFinished]
. Thats the way you can do all task with a non-blocking manner. For more information see the NSThread Class reference hereHappy Coding!