Objective c - NSTimer, CADisplayLink, Thread

二次信任 提交于 2019-12-13 07:37:24

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/11098311/objective-c-nstimer-cadisplaylink-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!