dispatch_after是在指定時間后,將執行加入到隊列中。
dispatch_after的用法如下:
dispatch_time_t time=dispatch_time(DISPATCH_TIME_NOW, 2ull *NSEC_PER_SEC); //設置時間2秒 dispatch_after(time, dispatch_get_main_queue(), ^{ //2秒后執行操作 //添加操作 });
也可以用以下寫法:
dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW, 2ull *NSEC_PER_SEC), dispatch_get_main_queue(), NULL, function);//function為自己定義的執行方法
dispatch_afer搭配hud使用起來效果更加
[SVProgressHUD showWithStatus:@"正在清理..."]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2ull * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [SVProgressHUD dismiss]; [SVProgressHUD showSuccessWithStatus:@"清理成功"]; });
參考例子