本文介绍了目标c-NSTimer,CADisplayLink,线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用倒计时计时器进行小游戏。我使用NSTimer,您可以获得更多时间还是更少时间,取决于您的操作。在为计时器添加或删除秒数时,时间标签出现了问题。问题出在我身上,我打电话给我的方法timerAction,当我添加或删除时间时,该方法会在标签中设置新时间,然后NSTimer也正在调用该方法。如果我不打电话给timerAction,我必须等待1秒钟才能使用NSTimer刷新标签。

I'm currently making a little game with a countdown timer. I use an NSTimer, you can get extra time or less time, depends on your action. When I'm adding or removing seconds to my timer I have an issue with the time label. The issue is due to me, I'm calling my method timerAction, who set the new time in the label when I'm adding or removing time and then the NSTimer is calling the method too. If I don't call timerAction, I must wait 1 second to refresh the label with NSTimer.

那么,我该怎么办才能解决我的问题?

So, what should I do to solve my issue ?

谢谢您的帮助。

推荐答案

首先,我想问一下您的问题码。

Firstly, I would like to ask about your code.

此外,在这种情况下,我会喜欢这种模式。

Besides, in this case, I will do like this pattern.

@property (nonatomic) NSInteger remainingTime;
@property (nonatomic) NSTimer *timer;

timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; 

- (void)timerAction {
    if (!countingFinish) {
       remainingTime --;
       //backend regular action
       [self frontEndUpdate];
    }else {
       [timer invalidate];
    }
}

- (bool)countingFinish {
    return (reamainingTime <= 0);
}

- (void)frontEndUpdate {
    //update time label, or other front end update action
}

- (void)addCountingRemaining:(NSInteger) _sec {
     remainingTime += _sec;
     [self frontEndUpdate];
}

btw,这是juz草案代码。希望对您有帮助

btw, this is juz draft code. hope it helps

这篇关于目标c-NSTimer,CADisplayLink,线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:01