你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> iOS 四種延時的方法

iOS 四種延時的方法

編輯:關於IOS

- (void)init51ios{
//延時的方法
//1:GCD延時 此方式在可以在參數中選擇執行的線程。是一種非阻塞的執行方式,沒有找到取消執行的方法。
double delay1=2.0;//設置延時時間
dispatch_time_t popTime=dispatch_time(DISPATCH_TIME_NOW, delay1 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"51ios.net" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:@"取消", nil];
[alert show];
});
//2:NSTimer延時,此方式要求必須在主線程中執行,否則無效。是一種非阻塞的執行方式,可以通過NSTimer類的- (void)invalidate;取消執行。
[NSTimer scheduledTimerWithTimeInterval:8.0f target:self selector:@selector(delayMethod2) userInfo:nil repeats:NO];
//3:PerformSelector延時
[self performSelector:@selector(delayMethod) withObject:nil afterDelay:5.0f];
//4:NSThread 延時
[NSThread sleepForTimeInterval:11.0f];
[self delayMethod3];
}
- (void)delayMethod{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"51ios.net延時" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:@"取消", nil];
[alert show];
}
- (void)delayMethod2{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"51ios.netNSTimer延時" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:@"取消", nil];
[alert show];
}
- (void)delayMethod3{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"51ios.netNSThread延時" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:@"取消", nil];
[alert show];
}

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved