【问题标题】:"disable" button-->method until operation is done“禁用”按钮->方法,直到操作完成
【发布时间】:2014-08-30 21:14:25
【问题描述】:

我正在使用通过精灵生成器按下按钮调用的以下方法。

- (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

}

正如您从 cmets 中看到的那样,我尝试运行过一次。效果很好,但是如果用户回到这个场景,它会被禁用,直到你重新启动应用程序。 我如何才能阻止此方法第二次执行,直到第一次完成。 我知道代码很粗糙,我只是在这里学习....

提前致谢。

【问题讨论】:

    标签: objective-c cocos2d-iphone spritebuilder


    【解决方案1】:

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

    添加另一个performSelector:withObject: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
    

    【讨论】:

    • 你能给我指出正确的方向来阅读一些相关的文献吗?我只是看不到它。谢谢。
    • @user2800989 我添加了一个例子。
    • 谢谢您的回复。它有很大帮助。如果你有时间,我还有一个问题。而不是使用afterDelay,我怎样才能仅在重新加载场景时重置它。 IE。如果用户去了下一个场景并决定回到这个场景?
    • 使用任何视图生命周期方法。 viewWillAppear:viewDidAppear:viewDidDisappear: 都可以。
    • 是的。我在按下添加评论的那一刻就明白了……该死的高级时刻:)。我很想投票给你的答案,但我还没有代表。对不起。
    【解决方案2】:

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

    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")
            }
        }
    }
    

    【讨论】:

    • 谢谢。我还没有继续快速。但我一定会把这个留在我的笔记里!
    • 是的,这是时尚的做法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 2015-06-10
    相关资源
    最近更新 更多