乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      關(guān)于OC中的幾種延遲執(zhí)行方式

       頭號碼甲 2022-01-18

      第一種:

      [UIView animateWithDuration:3 delay:3 options:1 animations:^{
              self.btn.transform = CGAffineTransformMakeTranslation(300, 400);
          } completion:^(BOOL finished) {
              NSLog(@"view animation結(jié)束");
       }];//不會阻塞線程,animations  block中的代碼對于是支持animation的代碼,才會有延時效果,對于不支持animation的代碼 則 不會有延時效果
       

      第二種:

      [NSThread sleepForTimeInterval:3];//阻塞線程,浪費性能 ,一般不推薦用。此方式在主線程和子線程中均可執(zhí)行。 建議放到子線程中,以免卡住界面,沒有找到取消執(zhí)行的方法。
      [self delayMethod];
      第三種:最常用
      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             
       });//定制了延時執(zhí)行的任務(wù),不會阻塞線程,在主線程和子線程中都可以,效率較高(推薦使用)。此方式在可以在參數(shù)中選擇執(zhí)行的線程。 是一種非阻塞的執(zhí)行方式, 沒有找到取消執(zhí)行的方法。
      第四種:
      [self performSelector:@selector(test) withObject:nil afterDelay:3];//此方式要求必須在主線程中執(zhí)行,否則無效。 是一種非阻塞的執(zhí)行方式.
      [[self class] cancelPreviousPerformRequestsWithTarget:self];//取消本類中執(zhí)行的performSelector:方法

      第五種:定時器

      1)NSTimer

      [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];//此方式要求必須在主線程中執(zhí)行,否則無效。 是一種非阻塞的執(zhí)行方式, 可以通過NSTimer類的- (void)invalidate;取消執(zhí)行。

      2)dispatch_source_t(比 NSTimer 更準的定時器),也可以在子線程中執(zhí)行,非阻塞執(zhí)行方式

      dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
          
      self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
          
      //開始時間
      dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, 3.0 * NSEC_PER_SEC);
          
      //間隔時間
      uint64_t interval = 2.0 * NSEC_PER_SEC;
          
      dispatch_source_set_timer(self.timer, start, interval, 0);
          
      //設(shè)置回調(diào)
      dispatch_source_set_event_handler(self.timer, ^{
           [self delayMethod];
           dispatch_suspend(self.timer);
      });
          
      //啟動timer
      dispatch_resume(self.timer);

       

       

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多