最近在做項目中碰到需要做個延時操作,于是把這個幾個簡單方法記錄下來以便以后使用方便; 如果有更好的方法歡迎添加一起完善;
1、NSimer
//1秒后執(zhí)行
NSTimer *_timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(_doTimer:) userInfo:nil repeats:NO];
2、系統(tǒng)
//延遲1s執(zhí)行
[self performSelector:@selector(_doTimer:) withObject:nil afterDelay:1];
3、GCD 延遲
/**
* 異步執(zhí)行 好處在于經(jīng)度高
* 參數(shù)1:延時的時間 dispatch_time 生成時間 納秒為計數(shù)單位
* 參數(shù)2:隊列
* 參數(shù)3:任務(wù) 并且異步執(zhí)行
* block 里邊是延遲后執(zhí)行的方法
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self _task];
});