Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

Objective c - NSTimer, CADisplayLink, Thread

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 ?

Thanks for your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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, this is juz draft code. hope it helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...