是一種非堵塞的運行方式,沒有找到取消運行的方法。
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:@"GCD延時" 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:@"PerformSelector延時" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:@"取消", nil];
[alert show];
}
- (void)delayMethod2{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"NSTimer延時" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:@"取消", nil];
[alert show];
}
- (void)delayMethod3{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"NSThread延時" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:@"取消", nil];
[alert show];
}