Count-Up Timer Required, iPhone Programming

2019-02-21 01:26发布

问题:

I am new to iPhone programming so am hoping someone can help me out here. I have searched the web, but can only find information on count down timers.

What I am looking to do is start a count up timer when a button is pressed and then stop it when a certain value drops by, say 5, and finally display that time. I can display values on screen once I have them, but getting the time in the first place is proving difficult for me.

I apologize if this is a simple question, but I look forward to reading your responses.

Thanks in advance, stu

回答1:

NSDate will provide the current date. You can use - (NSTimeInterval)timeIntervalSinceNow to get the time since the first call and now.



回答2:

There's no difference between an up-counter and down-counter. Just change the order of your subtraction.

UpcounterElapsedTime = UpcounterCurrentTime - UpcounterStartTime;
DowncounterElapsedTime = DownCounterStartTime - DownCounterCurrentTime;