延迟执行iOS应用程序

时间:2013-06-06 18:35:25

标签: ios objective-c delay execution

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

4 个答案:

答案 0 :(得分:10)

根据您正在做的事情的复杂程度,这个小小的片段就足够了:

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

答案 1 :(得分:2)

如果你的意思是一段时间,那么通常的做法是使用NSTimer在将来的某个时间触发,或者使用dispatch_after(),它可以将一个块分派给主队列或者在未来的某个时间,背景一个。

答案 2 :(得分:2)

大卫是对的。以下是我使用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];
}

答案 3 :(得分:1)

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

相关问题