您可以使用dispatch_after
在一定时间后执行块。
int64_t delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
/* code to be executed on the main queue after delay */
});
我将其称为时间调度块。
编辑:如何只发送一次。
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
/* code to be executed once */
});
所以在你的情况下:
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
int64_t delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
/* code to be executed on the main queue after delay */
})
});