问题描述
新手obj-c问题。
我正在为Appstore写一个简单的iPad演示文稿。我的任务是实现一个接一个地执行的几个方法,它们之间几乎没有暂停。主结构如下所示:
Newbie obj-c question.I am writing a simple iPad presentation not for Appstore. My task is to implement few methods executed one after another with little pauses between them. Main structure looks like this:
- 查看加载
- 暂停两秒钟,然后执行method1
- 暂停两秒钟,然后执行method2
- 暂停两秒钟,然后执行method3
等......
- view loads
- two seconds pause, then executing method1
- two seconds pause, then executing method2
- two seconds pause, then executing method3etc...
我从-viewDidLoad调用的第一个方法:
First method I am calling from -viewDidLoad:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(firstCountStarts) userInfo:nil repeats:NO];
此处一切正常,方法在视图加载后2秒开始。
从method1里面我尝试以相同的方式调用方法2,但它开始与method1同时执行。同样的方法触发了method3(从method2调用)以及它们之后根本没有执行的所有方法。我试图将所有这些方法放在-ViewDidLoad中,并用延迟来调用它们:
Everything is ok here, method starts 2 seconds after view loads.From inside method1 I try to call method 2 in the same way but it start to execute simultaneously with method1. Same way triggered method3 (called from method2) and all methods after them not executed at all. I tried to situate all this methods in -ViewDidLoad and to call them with delays:
[self method1];
[self performSelector:@selector(method2) withObject:nil afterDelay:2];
[self performSelector:@selector(method3) withObject:nil afterDelay:4];
etc...
但是在method2调用之后所有方法都没有执行。如果我理解线程中的问题。我是否需要使用GCD在不同的队列中执行方法?或者也许是其他问题?
But after method2 is calling all methods after didn't executed. If I understand right the issue in threads. Do I need to use GCD to execute methods in different queues? Or maybe problem in else?
谢谢,同事们!
推荐答案
您可以将这些添加到NSOperation队列...
You could add these to an NSOperation queue...
NSOperationQueue *queue = [NSOperationQueue new];
queue.maxConcurrentOperationCount = 1;
[queue addOperationWithBlock:^{
[self method1];
}];
[queue addOperationWithBlock:^{
[NSThread sleepForTimeInterval:2.0];
[self method2];
}];
[queue addOperationWithBlock:^{
[NSThread sleepForTimeInterval:2.0];
[self method3];
}];
...
然后只会在之前的每一个上运行一个已经完成,并为你提供了2秒的延迟。
This will then run each one only after the previous one has finished and put the 2 second delay in for you.
小心使用它来做一个UI的东西。这将在后台线程中运行,因此您可能需要处理它。
Careful about using this to do an UI stuff though. This will run in a Background thread so you may need to deal with that.
也许这可能会更好,你可以通过继承NSOperation来做到这一点,但这是很多工作没有多大好处。
Maybe this might work better you could do it by subclassing NSOperation but that's a lot of work for not much benefit.
从你想要的地方运行这个,我建议将所有这些放入一个名为setUpQueue的函数中。
Run this from where ever you want, I suggest putting all this into a function called setUpQueue or something.
然后从viewWillAppear或viewDidLoad或其他地方,按下按钮等...执行...
Then from viewWillAppear or viewDidLoad or somewhere else, on a button press, etc... do...
[self setUpQueue];
所有你需要做的就是在队列中添加东西,队列将自行管理。
All you have to do is add stuff to the queue, the queue will then manage itself.
这篇关于在执行之间暂停执行一个接一个的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!