ios4 – iPhone:如何编码减少倒数计时器?

ios4 – iPhone:如何编码减少倒数计时器?,第1张

概述我想用UILabel显示一个倒数计时器,它将从5开始,每秒减少1,如: 五 4 3 2 1 最后在标签达到0时隐藏标签. 我尝试使用NSTimer scheduledTimerWithTimeInterval对其进行编码,但失败了. 请帮我. 我只是在做那样的事情.我的代码中有一个名为timeReamin的UILabel.它每秒更新一次,直到达到0,然后显示警报.我必须警告你,由于计时器与你的UI @H_404_4@ 我想用UILabel显示一个倒数计时器,它将从5开始,每秒减少1,如:


4
3
2
1

最后在标签达到0时隐藏标签.

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

请帮我.

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

- (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中,您需要一行如下:

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

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

NSautoreleasePool *pool = [[NSautoreleasePool alloc] init];NSRunLooP* runLoop = [NSRunLoop currentRunLoop];// start timergameTimer = [[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.然后注意最后有一个新的声明:

[runLoop run];

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

@H_404_4@ @H_404_4@ @H_404_4@ @H_404_4@ 总结

以上是内存溢出为你收集整理的ios4 – iPhone:如何编码减少倒数计时器?全部内容,希望文章能够帮你解决ios4 – iPhone:如何编码减少倒数计时器?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1007166.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存