2014-08-30 3 views
2

sprite builder를 통해 버튼을 눌러 호출하는 다음 메소드를 사용하고 있습니다.작업이 완료 될 때까지 "disable"버튼 -> 메서드

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

} 

위의 코멘트에서 볼 수 있듯이 한 번 실행 해 보았습니다. 잘 작동하지만 사용자가이 장면으로 돌아 오면 앱을 다시 시작할 때까지 사용할 수 없습니다. 어떻게하면이 메서드가 처음 수행 될 때까지 두 번째로 실행되지 않도록 차단할 수 있습니다. 코드가 거칠다는 것을 알고 있습니다. 나는 여기서 배우고 있습니다 ....

감사합니다.

답변

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 
+0

당신이 몇 가지 문헌을 읽고 올바른 방향으로 날 지점 수 있습니다. 나는 그것을 볼 수 없다. 감사. – user2800989

+0

@ user2800989 예제를 추가했습니다. – nhgrif

+0

답장을 보내 주셔서 감사합니다. 그것은 많은 도움이되었습니다. 시간이 있으면 질문이 하나 더 있습니다. afterDelay를 사용하는 대신 장면을 다시로드 할 때만 어떻게 재설정 할 수 있습니까? 즉. 사용자가 다음 장면에 가서이 장면으로 돌아 오기로 결정한 경우? – user2800989

0

간단한 방법은 (스위프트에)와 같은 직렬 NSOperationQueue를 사용하는 것입니다 :

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

감사합니다. 나는 아직 빨리 움직이지 않았다. 그러나 나는 이것을 나의 노트에 명확하게 보관할 것이다! – user2800989

+0

예, 멋진 방법입니다. – augustzf