基本iphone计时器示例 [英] Basic iphone timer example

查看:127
本文介绍了基本iphone计时器示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我在网上搜索过,甚至在几本书中找到答案,因为我无法理解NSTimer的苹果文档。我试图在同一视图上实现2个定时器,每个定时器都有3个按钮(START - STOP - RESET)。

Okay, I have searched online and even looked in a couple of books for the answer because I can't understand the apple documentation for the NSTimer. I am trying to implement 2 timers on the same view that each have 3 buttons (START - STOP - RESET).

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

The first timer counts down from 2 minutes and then beeps.

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

The second timer counts up from 00:00 indefinitely.

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

I am assuming that all of the code will be written in the methods behind the 3 different buttons but I am completely lost trying to read the apple documentation. Any help would be greatly appreciated.

推荐答案

基本上你想要的是每1秒触发一次的事件,或者可能是1 /第10个间隔,当计时器滴答时,您将更新您的UI。

Basically what you want is an event that fires every 1 second, or possibly at 1/10th second intervals, and you'll update your UI when the timer ticks.

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

The following will create a timer, and add it to your run loop. Save the timer somewhere so you can kill it when needed.


- (NSTimer*)createTimer {

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

现在为计时器刻度写一个处理程序:

Now write a handler for the timer tick:


- (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计时器示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆