gpt4 book ai didi

iphone - 基本的 iPhone 计时器示例

转载 作者:太空狗 更新时间:2023-10-30 03:11:28 25 4
gpt4 key购买 nike

好吧,我在网上搜索过,甚至在几本书中寻找答案,因为我无法理解 NSTimer 的苹果文档。我正在尝试在同一个 View 上实现 2 个计时器,每个计时器都有 3 个按钮(开始 - 停止 - 重置)。

第一个计时器从 2 分钟开始倒计时,然后发出哔哔声。

第二个计时器从 00:00 开始无限期地计数。

我假设所有代码都将写在 3 个不同按钮后面的方法中,但我完全迷失了阅读苹果文档。任何帮助将不胜感激。

最佳答案

基本上您想要的是每 1 秒或可能以 1/10 秒的间隔触发的事件,并且您将在计时器计时时更新您的 UI。

以下将创建一个计时器,并将其添加到您的运行循环中。将计时器保存在某个地方,以便您可以在需要时终止它。


- (NSTimer*)createTimer {

// create timer on run loop
return [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTicked:) userInfo:nil repeats:YES];
}

现在为计时器滴答写一个处理程序:

- (void)timerTicked:(NSTimer*)timer {    // decrement timer 1 … this is your UI, tick down and redraw    [myStopwatch tickDown];    [myStopwatch.view setNeedsDisplay];     // increment timer 2 … bump time and redraw in UI    …}

If the user hits a button, you can reset the counts, or start or stop the ticking. To end a timer, send an invalidate message:


- (void)actionStop:(id)sender {

// stop the timer
[myTimer invalidate];
}

希望这对你有帮助。

关于iphone - 基本的 iPhone 计时器示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3041256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com