按下开始游戏后,为我的NSTimer添加延迟

时间:2014-06-01 21:15:40

标签: ios delay nstimer

在我开始游戏的按钮上,我有一些NSTimers来滚动,我想为这些定时器添加延迟,但不太确定如何。

任何建议?

以下是包含1个NSTimers的按钮

-(IBAction)StartGame:(id)sender{

    StartGame.hidden = YES;



  backgroundMovement = [NSTimer scheduledTimerWithTimeInterval:0.06 target:self selector:@selector(backgroundMoving)

                                                   userInfo:nil repeats:YES];

}

谢谢

3 个答案:

答案 0 :(得分:4)

当您致电[NSTimer scheduledTimerWithTimeInterval:...]时,时间间隔 延迟。您将在指定的时间间隔后回叫。

或者,请致电performSelector:withObject:afterDelay:

但请注意,您提出的架构存在严重缺陷。具有.06间隔的重复计时器是可怕的想法,并且不止一个是恶劣的想法。你需要完全重新考虑这一点。考虑使用真实动画,显示链接或精灵工具包。或者其他的东西。什么,真的。

答案 1 :(得分:0)

如果您只想添加延迟一次,那么在启动时,您可以实施- (void)startTimers方法,在其中创建并启动计时器,然后执行

[self performSelector:@selector(startTimers) withObject:nil afterDelay:1.0f];

IBAction

^不要复制粘贴performSelector方法,因为我可能打错了,因为我没有打开Xcode进行验证。

答案 2 :(得分:0)

执行此操作的选项:

1)使用performSelector使用延迟调用方法。在该方法中,您可以创建NSTimer

[self performSelector:@selector(createTimer_backgroundMovement) withObject:nil
                                                                afterDelay:0.1];

2)如果延迟取决于某个操作,您可以在标志中使用此信息,该标志可以在backgroundMoving方法中使用:

-(void) backgroundMoving {
    if (!blAction_whichEnablesTimer) {
         return;
    }

    // your backgroundMoving timer code
}

3)考虑用例如移动视图。阻止动画。使用块动画也可以使用延迟。请参阅doc:https://developer.apple.com/library/ios/documentation/windowsviews/conceptual/viewpg_iphoneos/animatingviews/animatingviews.html