0

我有四个运行良好的 NSTimer。唯一的问题是两个应该开火,然后另外两个应该在前两个中间开火。这是我的代码:

   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 触发。

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

- - - - - 编辑 - - - - - - -

@MDT 那行得通,但有一个问题。我所做的是将上面的 initTimer1 和 animateTimer1 的代码放在名为 initTimer1Wrapper 和 animateTimer1Wrapper 的新函数中。然后我做了你的代码[self performSelector:@selector(initText1Wrapper) withObject:nil afterDelay:1.0];并复制了它,但将选择器更改为 animateText1Wrapper。发生的情况是 initText 和 textGlow 将按计划触发,然后按计划 initTimer1 和 animateTimer1 将触发一秒,但意外发生的是 initText 和 textGlow 没有完成剩余的一秒;动画只是停止。我相信这是因为它们是 UIView 动画,[UIView beginAnimation:@"name" context:nil]并且[UIView commitAnimations]超过 1 个 UIView 动画不能同时运行。

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

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

另外,如果您知道这很重要,我将使用 UIGestureRecognizer 关闭此动画。就像是:

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

2 回答 2

1

尝试将这样的东西放在里面initTexttextGlow

[self performSelector:@selector(initText1) withObject:nil afterDelay:1.0];
于 2012-06-03T05:10:56.013 回答
0

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

于 2012-06-03T05:16:02.203 回答