例えば、画像のダウンロード状況をプログレスで表示していて、プログレスが100%の状態を一定時間ユーザーに見せておきたい場合、以下のように記述します。
※画像のダウンロード処理はSDWebImageを使用しています。
Blocksで遅延実行する
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:photo.imamgeURL
options:0
progress:
^(NSInteger receivedSize, NSInteger expectedSize) {
float progress = [@(receivedSize) floatValue]/[@(expectedSize) floatValue];
progressView.progress = progress;
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if( error ){
return;
}
if( finished ){
//dispatch_afterを使って遅延させる
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[progressView dismiss:YES];
});
}
}];