#import “CountdownViewController.h”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// default countdown time in seconds
static int defaultBufferSeconds = 10;

@interface CountdownViewController ()

@property (nonatomic, strong) UILabel *timerLabel;

@end

@implementation CountdownViewController {
// to keep a reference for current counting
int countdownMilliseconds;
}

@synthesize timerLabel = _timerLabel;

- (void)viewDidLoad
{
[super viewDidLoad];

// display countdown on this label
self.timerLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 240, 40)];
self.timerLabel.textAlignment = NSTextAlignmentCenter;
[self.countdownView addSubview:self.timerLabel];

// start countdown
[self startTimer];
}

- (void)startTimer
{
countdownMilliseconds = defaultBufferSeconds * 1000; // reset the current counting value
// NSTimeInterval is in "second", now schedule it to every 10 milliseconds
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateCountdown:) userInfo:nil repeats:YES];
}


- (void)updateCountdown:(NSTimer *)timer
{
int seconds, milliseconds;

// don't forget to update the current counting value
countdownMilliseconds -= (timer.timeInterval * 1000);

seconds = countdownMilliseconds / 1000;

// since we only want to show 2 digits in milliseconds, so have to divide by 10
milliseconds = countdownMilliseconds % 1000;
milliseconds /= 10;

self.timerLabel.text = [NSString stringWithFormat:@"Countdown: %02d seconds %02d milliseconds", seconds, milliseconds];

// once current counting value reach 0, then stop the timer
if (countdownMilliseconds <= 0) {
[timer invalidate];
timer = nil;
}
}

@end

References: