iPhone:如何编码减少倒数计时器? [英] iPhone: How to code a reducing countdown timer?

查看:72
本文介绍了iPhone:如何编码减少倒数计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 UILabel 显示倒数计时器,该计时器将从5开始,每秒减少1 ,如:

I want to show a countdown timer using a UILabel which will start from 5 and reduces by 1 every second like:

5
4
3
2
1

5 4 3 2 1

最后隐藏它达到0时的标签。

and finally hides the label when it reaches 0.

我尝试使用 NSTimer scheduledTimerWithTimeInterval 对其进行编码,但失败了。

I tried to code it using NSTimer scheduledTimerWithTimeInterval but failed miserably.

请帮助我。

推荐答案

我只是做了类似的事情。我的代码中有一个名为timeReamin的UILabel。它每秒更新一次,直到达到0,然后显示警报。我必须警告你,由于计时器与你的UI在同一个线程上运行,你可能会遇到一些抖动。我还没有解决这个问题,但这适用于简单的计时器。这是我正在使用的代码:

I was just doing something like that. I have a UILabel that is called timeReamin in my code. It's updated every second until it reaches 0 and then an alert is displayed. I must warn you that since the timer runs on the same thread as your UI, you may experience some jitter. I have not solved that problem yet, but this works for simple timers. Here is code that I am using:

- (void)createTimer {       
    // start timer
    gameTimer = [[NSTimer timerWithTimeInterval:1.00 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES] retain];
    [[NSRunLoop currentRunLoop] addTimer:gameTimer forMode:NSDefaultRunLoopMode];
    timeCount = 5; // instance variable
}

- (void)timerFired:(NSTimer *)timer {
    // update label
    if(timeCount == 0){
        [self timerExpired];
    } else {
        timeCount--;
        if(timeCount == 0) {
            // display correct dialog with button
        [timer invalidate];
        [self timerExpired];
         }
    }
    timeRemain.text = [NSString stringWithFormat:@"%d:%02d",timeCount/60, timeCount % 60];
}


- (void) timerExpired {
   // display an alert or something when the timer expires.
}

找出一个消除抖动的线程解决方案。在viewDidLoad方法或applicationDidFinishLaunching中,您需要一行如下:

Figured out a threaded solution that removes the jitter. In the viewDidLoad method or applicationDidFinishLaunching, you need a line such as:

[NSThread detachNewThreadSelector:@selector(createTimer) toTarget:self withObject:nil];

这将使用createTimer方法启动一个线程。但是,您还需要更新createTimer方法:

This will launch a thread using the createTimer method. However, you also need to update the createTimer method as well:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
// start timer
gameTimer = [[NSTimer timerWithTimeInterval:1.00 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES] retain];
[[NSRunLoop currentRunLoop] addTimer:gameTimer forMode:NSDefaultRunLoopMode];
[runLoop run];
[pool release];

这大部分都是标准的线程入口例程。该池用于线程使用的托管内存策略。如果您使用垃圾收集,则不需要这样做,但它不会受到伤害。 runloop是一个事件循环,连续执行以在每秒经过一段时间后生成事件。您的主线程中有一个自动创建的runloop,这是一个特定于此新线程的runloop。然后注意最后有一个新的声明:

Most of this is standard thread entry routine stuff. The pool is used for managed memory strategies employed by the thread which. This is not needed if you are using garbage collection, but it does not hurt. The runloop is an event loop that is executed continuously to generate the events when the time elapses every second. There is a runloop in your main thread that is created automatically, this is a runloop that is specific to this new thread. Then notice that there is a new statement at the end:

[runLoop run];

这可确保线程无限期执行。您可以管理计时器,例如重新启动计时器或将其设置为其他方法中的不同值。我在我的代码中的其他位置初始化我的计时器,这就是为什么该行已被删除。

This ensures that the thread will execute indefinitely. You can manage the timer, such as restarting it or setting it for different values within the other methods. I initialize my timer at other places in my code and that is why that line has been removed.

这篇关于iPhone:如何编码减少倒数计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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