Objective c &引用;禁用";按钮-->;方法,直到操作完成

Objective c &引用;禁用";按钮-->;方法,直到操作完成,objective-c,cocos2d-iphone,spritebuilder,Objective C,Cocos2d Iphone,Spritebuilder,我使用的是通过按下sprite builder中的按钮来调用的以下方法 - (void)method { //static dispatch_once_t pred; // //dispatch_once(&pred, ^{ // run only once code below [self performSelector:@selector(aaa) withObject:nil afterDelay:0.f]; [self performSelector:@

我使用的是通过按下sprite builder中的按钮来调用的以下方法

- (void)method {

//static dispatch_once_t pred;    //
//dispatch_once(&pred, ^{         // run only once code below

[self performSelector:@selector(aaa) withObject:nil afterDelay:0.f];
[self performSelector:@selector(bbb) withObject:nil afterDelay:1.f];
[self performSelector:@selector(ccc) withObject:nil afterDelay:1.5f];
[self performSelector:@selector(ddd) withObject:nil afterDelay:4.f];
[self performSelector:@selector(eee) withObject:nil afterDelay:4.5f];

CCLOG(@"Received a touch");

//}); //run only once code above

}
正如你从评论中看到的,我试过运行它一次。这很好,但如果用户返回此场景,则在您重新启动应用程序之前,它将被禁用。 在第一次执行之前,我如何阻止第二次执行此方法。 我知道代码很粗糙,我只是在这里学习


提前感谢。

添加一个
BOOL
实例变量,作为此操作是否发生的标志。一旦方法启动,请检查标志。如果需要执行,请设置标志

添加另一个
performSelector:withObject:afterDelay:
,它调用一个方法将标志重置回原位



更简单的方法是使用串行NSOperationQueue(在Swift中):


你能给我指一下正确的方向,让我读一些这方面的文献吗。我就是看不见。谢谢。@user2800989我添加了一个示例。谢谢您的回复。这帮了大忙。如果你有时间的话,我还有一个问题。不使用afterDelay,如何仅在重新加载场景时重置它。例如,如果用户转到下一个场景并决定返回到此场景,请使用任何视图生命周期方法<代码>视图将出现:,
视图显示:
,或
视图消失:
都可以。是的。我一按下“添加评论”按钮就发现。。。。该死的高级时刻:)。我很想投你的票,但我还没有代表。对不起,谢谢。我还没有转到斯威夫特。但我一定会把这个记在我的笔记里!是的,这是一种时尚的方式。
@implementation SomeClass {
    BOOL _onceAtATime;
}

- (void)method {
    @synchronized(self) {
        if (!_onceAtATime) {
            _onceAtATime = YES;

            // do all the stuff you need to do

            [self performSelector:@selector(resetOnceAtATime) 
                       withObject:nil 
                       afterDelay:delay];
             // where delay is sufficiently long enough for all the code you
             // are executing to complete
        }
    }
}

- (void)resetOnceAtATime {
    _onceAtATime = NO;
}

@end
class ViewController: UIViewController {

    let queue: NSOperationQueue

    required init(coder aDecoder: NSCoder) {
        queue = NSOperationQueue()
        queue.maxConcurrentOperationCount = 1
        super.init(coder: aDecoder)
    }

    @IBAction func go(sender: AnyObject) {
        if (queue.operationCount == 0) {
            queue.addOperationWithBlock() {
                // do the first slow thing here
            }
            queue.addOperationWithBlock() {
                // and the next slow thing here
            }
            // ..and so on
        }
        else {
            NSLog("busy doing those things")
        }
    }
}