LoginSignup
19

More than 5 years have passed since last update.

dispatch_afterを使って遅延実行する

Posted at

例えば、画像のダウンロード状況をプログレスで表示していて、プログレスが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];
             });
         }

     }];

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19