iOS:OC的定時器方法,延時方法
方法1:performSelector
// 1.5s後自動調用self的hideHUD方法
[self performSelector:@selector(hideHUD) withObject:nil afterDelay:1.5];
參數分析:
@selector(hideHUD):選擇延時後調用的方法,hideHUD是自定義的方法。
withobject:傳入的參數,若沒有,則爲nil。
afterDelay:需要延時的時間
方法2:GCD
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 1.5s後自動執行這個block裏面的代碼
self.hud.alpha = 0.0;<pre name="code" class="objc">});
參數分析:延時1.5秒之後,執行block裏面的代碼。在這裏只需要設置延時時間和延時之後需要執行的代碼即可。
方法3:NSTimer
// 1.5s後自動調用self的hideHUD方法
[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(hideHUD) userInfo:nil repeats:NO];
// repeats如果爲YES,意味着每隔1.5s都會調用一次self的hidHUD方法