交错的NSTimer火灾

时间:2012-06-03 05:03:07

标签: objective-c ios core-animation nstimer uiviewanimation

我有四个NSTimers可以正常工作。唯一的问题是两个应该开火,然后另外两个应该在前两个中间开火。这是我的代码:

   initTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector: @selector(initText) userInfo:nil repeats:YES];
   animateTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector: @selector(textGlow) userInfo:nil repeats:YES];
   initTimer1 = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector: @selector(initText1) userInfo:nil repeats:YES];
   animateTimer1 = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector: @selector(textGlow1) userInfo:nil repeats:YES];

initText和initText1方法创建一个带有延迟实例化的UILabel,并将它们放在同一位置的屏幕上。 textGlow和textGlow1方法为文本设置动画,使其变大并逐渐消失。我希望initText1和textGlow1在initText和textGlow的中间触发。例如,假设这些方法中的每一个都需要2秒(我将动画持续时间设置为2秒)。我希望触发initText和textGlow,然后在initText1和textGlow1中触发一秒钟。

感谢。如果您需要我的其他任何代码来帮助回答问题,请询问。

---------- EDIT -------------

@MDT虽然有效,但有问题。我所做的是将initTimer1和animateTimer1的代码放在名为initTimer1Wrapper和animateTimer1Wrapper的新函数中。然后我完成了你的代码[self performSelector:@selector(initText1Wrapper) withObject:nil afterDelay:1.0];并重复了它,但将选择器更改为animateText1Wrapper。会发生什么是initText和textGlow将按计划启动,然后按计划启动initTimer1和animateTimer1将启动一秒钟,但意外发生的是initText和textGlow没有完成剩余的一秒;动画就此停止。我相信这是因为它们是带有[UIView beginAnimation:@"name" context:nil][UIView commitAnimations]的UIView动画,并且超过1个UIView动画无法同时运行。

要解决这个问题,我应该使用核心动画(我对此一无所知)还是有其他解决方案?感谢。

---------- EDIT2 -------------

此外,如果您知道这一点很重要,我将使用UIGestureRecognizer关闭此动画。 类似的东西:

if(screenIsTapped){
    [initTimer invalidate];
    [animateTimer invalidate];
    [initTimer1 invalidate];
    [animateTimer1 invalidate];
}

2 个答案:

答案 0 :(得分:1)

尝试在initTexttextGlow内放置此类内容。

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

答案 1 :(得分:0)

我认为你应该使用2个定时器而不是4个。在方法中,initText和initText1分别使用performSelector:withObject:afterDelay:调用textGlow和textGlow1,无论你想要什么延迟。而且,你真的希望这些计时器重复吗?你想继续创建新的UILabel吗?