调用函数与间隔,但直到一段时间过去了iPhone



我需要在 30 秒内每 .025 秒拨打一次电话。这是我的.025秒计时器。

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

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

如果你只希望它继续 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];
}

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

如果您想在 30 秒后调用一次GetScreen:(这应该按照正常的 Obj-C 约定getScreen:(,请使用以下命令:

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

最新更新