本文嘗試模仿 iOS 系統「時鐘」裡的秒表功能,做出一個簡單的秒表小應用,如下圖所示:

主要要實作的功能有兩項:
1. 透過 start/stop 鍵來控制計時的開始與停止。
2. 透過 reset/lap 鍵來達成歸零與計次。
動手之前有兩個問題值得先想清楚:
1. 時間該用什麼方式來表示(其實有很多種思路)。
2. 計次資料需要倒序排列,也就是「計次 1」的資料顯示在最底端,往上依序是計次 2、計次 3……
得益于 ARC,我們省下了自行管理記憶體的大部分工作,寫起來比 C++ 輕鬆許多。以下是我的實作程式碼:
- (IBAction)startOrstop:(UIButton *)sender
{
// 點擊切換按鈕背景圖
UIImage *newImage = (checked) ? [UIImage imageNamed:@"red.png"] : [UIImage imageNamed:@"green.png"];
[leftBtn setBackgroundImage:newImage forState:UIControlStateNormal];
NSString *titlel = (checked) ? (@"Stop") : (@"Start");
[leftBtn setTitle:titlel forState:UIControlStateNormal];
NSString *titler = (checked) ? (@"Lap") : (@"Reset");
[rightBtn setTitle:titler forState:UIControlStateNormal];
if (checked) // start
{
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
} else { // stop
[timer invalidate];
}
checked = !checked;
}
- (IBAction)resetOrLap:(UIButton *)sender
{
static NSInteger count = 1;
if (checked) // reset
{
time = time_lap = 0.0;
timestr = [NSString stringWithFormat:@"00:00.0"];
[label setText:timestr];
list_time = list_lap = nil;
count = 1;
[tableview reloadData];
} else { // lap
if (list_time == nil) {
list_time = [[NSArray alloc] initWithObjects:timestr_lap, nil];
list_lap = [[NSArray alloc] initWithObjects:[NSString stringWithFormat:@"%d", count++], nil];
} else {
NSArray *array = [[NSArray alloc] initWithObjects:timestr_lap, nil];
list_time = [array arrayByAddingObjectsFromArray:list_time];
array = [[NSArray alloc] initWithObjects:[NSString stringWithFormat:@"%d", count++], nil];
list_lap = [array arrayByAddingObjectsFromArray:list_lap];
}
time_lap = 0;
[tableview reloadData];
}
}
- (float)updateTime
{
time += 0.1;
time_lap += 0.1;
timestr = [NSString stringWithFormat:@"%02d:%04.1f", (int)(time / 60), time - (60 * (int)(time / 60))];
timestr_lap = [NSString stringWithFormat:@"%02d:%04.1f", (int)(time_lap / 60), time_lap - (60 * (int)(time_lap / 60))];
[label setText:timestr];
return time;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [list_time count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableViewIdentifier = @"tableViewIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:tableViewIdentifier];
}
NSUInteger row = [indexPath row];
cell.detailTextLabel.text = [list_time objectAtIndex:row];
cell.detailTextLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:25.0];
cell.detailTextLabel.textAlignment = NSTextAlignmentCenter;
NSString *text = [[NSString alloc] initWithFormat:@"lap %@", [list_lap objectAtIndex:row]];
cell.textLabel.text = text;
return cell;
}
執行後的效果如下:



這個版本仍有兩個地方可以再優化:
1. 計時的運算與 UI 事件最好分在不同執行緒處理,這裡為了求快偷了個懶。
2. 時間的表示方式也偏簡陋,並未採用標準的「分、秒進位」表示法。