匿名函数/块的NSTimer

需要实现安排三个小事件,但是不用给每个单独开一个功能。怎么样用NStimer实现?

[NSTimer scheduledTimerWithTimeInterval:gameInterval  
         target:self selector:@selector(/* I simply want to update a label here */) 
         userInfo:nil repeats:NO];

你可以用上dispatch_after,可以帮你实现和NSTimer和block执行类似的。

示例代码:

int64_t delayInSeconds = gameInterval; // Your Game Interval as mentioned above by you

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);

dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

    // Update your label here. 

});