【问题标题】:Calling function with interval but until some time passed Iphone以间隔调用函数,但直到经过一段时间 Iphone
【发布时间】:2012-05-17 18:02:35
【问题描述】:

我需要在 30 秒的持续时间内每 0.025 秒拨打一次电话。这是我的 0.025 秒计时器。

NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 0.025 target: self
                    selector: @selector(GetScreen:) userInfo: nil repeats:YES ];

如何将其限制为 30 秒的持续时间?

【问题讨论】:

  • 仅在 30 秒内是什么意思?你只想要一次吗?
  • 因为现在当我调用我编写的这个函数时,将每 0,025 调用一次,直到应用程序被关闭。我想每 0,025 调用一次,但在 30 秒内我不想调用这个函数,所以我想做的是调用它 30*0,025 次。

标签: iphone nstimer scheduler intervals


【解决方案1】:

如果您只希望它持续 30 秒,那么您只需让一个成员变量递增,直到达到 30 秒然后您关闭计​​时器:

CGFloat timerTotal_;

//initialize it where you create your timer
timerTotal_ = 0.0f;

//every time your GetScreen: is called
timerTotal_+= .025;

if(timerTotal_ > 30)
{
    [myTimer invalidate];
}

【讨论】:

    【解决方案2】:

    如果您试图等待某个条件满足,请不要使用计时器来执行此操作。使用委托模式、通知/观察者或信号量等。

    如果你想在 30 秒后调用一次 GetScreen:(按照正常的 Obj-C 约定应该是 getScreen:),使用这个:

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

    【讨论】:

    • 我想调用 getScreen 0,025*30 次,但有确切的时间间隔......你写的不是答案;/
    猜你喜欢
    • 1970-01-01
    • 2011-04-12
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    相关资源
    最近更新 更多