NSTimer Decrease time by seconds / milliseconds

I am developing a QuiZ application. To do this, you need a countdown timer in which I want to start the timer with a frequency of 75: 00.00 (mm: ss.SS) and reduce it to 00: 00.00 (mm: ss.SS) by reducing 1 millisecond. I want to show a warning that Time UP! when the time reaches 00: 00.00 (mm: ss.SS).

I show the time by following the link

Stopwatch Using NSTimer Incorrectly Pauses Display Time

+6
source share
2 answers

Here are simple solutions to your problem.

ads

@interface ViewController : UIViewController { IBOutlet UILabel * result; NSTimer * timer; int currentTime; } - (void)populateLabelwithTime:(int)milliseconds; -(IBAction)start; -(IBAction)pause; @end 

Definition

 - (void)viewDidLoad { [super viewDidLoad]; currentTime = 270000000; // Since 75 hours = 270000000 milli seconds } -(IBAction)start{ timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES]; } -(IBAction)pause{ [timer invalidate]; } - (void)updateTimer:(NSTimer *)timer { currentTime -= 10 ; [self populateLabelwithTime:currentTime]; } - (void)populateLabelwithTime:(int)milliseconds { int seconds = milliseconds/1000; int minutes = seconds / 60; int hours = minutes / 60; seconds -= minutes * 60; minutes -= hours * 60; NSString * result1 = [NSString stringWithFormat:@"%@%02dh:%02dm:%02ds:%02dms", (milliseconds< 0?@ "-":@""), hours, minutes, seconds,milliseconds%1000]; result.text = result1; } 
+13
source

Use the following code to decrease the timer.

 double dblRemainingTime=60; //1 minute if(dblRemainingTime >0) { dblRemainingTime -= 1; int hours,minutes, seconds; hours = dblRemainingTime / 3600; minutes = (dblRemainingTime - (hours*3600)) / 60; seconds = fmod(dblRemainingTime, 60); [lblRemainingTime setText:[NSString stringWithFormat:@"%02d:%02d",minutes, seconds]]; } else alert('Time up buddy'); 
+3
source

Source: https://habr.com/ru/post/913775/


All Articles