【问题标题】:Delay execution iOS app延迟执行iOS应用
【发布时间】:2013-06-06 18:35:25
【问题描述】:

我正在寻找一种简单直接的方法来延迟 iOS 应用程序中多个帧的执行,这可能吗?

【问题讨论】:

  • 不清楚您在问什么,但也许NSTimer scheduledTimerWithTimeInterval 适合您的需求?
  • 请注意您的术语,当您谈论“延迟多个”时,我认为您可能来自另一个平台。你的意思是延迟一段时间。

标签: ios objective-c delay execution


【解决方案1】:

根据您正在做的事情的复杂性,这个小 sn-p 就足够了:

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    //code to be executed on the main queue after delay
});

否则,您可以查看NSTimer 来安排一些要执行的代码。您甚至可以重复一次或多次。

例如可以这样使用:

[NSTimer scheduledTimerWithTimeInterval:5.0 
                                 target:self 
                               selector:@selector(myMethod) 
                               userInfo:nil 
                                repeats:NO];

如需了解更多关于如何使用它的信息,请查看this post,当然,请查看documentation

【讨论】:

    【解决方案2】:

    如果你指的是某个恒定的时间,通常的方法是使用 NSTimer 在未来的某个时间触发,或者使用 dispatch_after(),它可以将一个块分派到主队列或未来某个时间的背景。

    【讨论】:

      【解决方案3】:

      大卫是正确的。这是我如何使用 NSTimer 的一些代码。

      -(void)startAnimating {
          [NSTimer scheduledTimerWithTimeInterval:.3 target:self selector:@selector(animateFirstSlide) userInfo:nil repeats:NO];
          self.pageTurner = [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(turnPages) userInfo:nil repeats:YES];
      }
      
      -(void)animateFirstSlide {
          [self animateSlideAtIndex:0];
      }
      
      -(void)stopPageTurner {
          [self.pageTurner invalidate];
      }
      

      【讨论】:

      • 谢谢大家的回复,我现在有很多可以尝试的。
      【解决方案4】:

      您可以使用[NSTimer scheduledTimerWithTimeInterval: target: selector:] 方法,也可以使用内置方法wait(time_in_seconds);
      希望这会对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-25
        • 2018-02-25
        • 2011-09-23
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多