程序的bug就是,59秒后没有马上进位,而是跳到60然后再进位。
//
// MainViewController.m
// timerDemo3
//
// Created by chensh on 14-2-10.
// Copyright (c) 2014年 chensh. All rights reserved.
//
#import "MainViewController.h"
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UILabel *lab;
NSTimer *timer;
CGFloat timeCount;
CGFloat lapTimeCount;
UIButton *ssBtn;
UIButton *lrBtn;
UITableView *_tableView;
NSMutableArray *dataArr;
}
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self viewCreated];
}
- (void)viewCreated
{
dataArr = [[NSMutableArray alloc]init];
lab = [[UILabel alloc]init];
lab.textColor = [UIColor whiteColor];
lab.backgroundColor = [UIColor blackColor];
lab.font = [UIFont boldSystemFontOfSize:23];
lab.text = @" 00:00.0 ";
[lab sizeToFit];
lab.frame = CGRectMake(160 - lab.frame.size.width/2, 50, lab.frame.size.width, lab.frame.size.height);
[self.view addSubview:lab];
ssBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[ssBtn setTitle:@"Start" forState:UIControlStateNormal];
[ssBtn setTitle:@"Stop" forState:UIControlStateSelected];
[ssBtn addTarget:self action:@selector(startOrStopTimer) forControlEvents:UIControlEventTouchUpInside];
ssBtn.adjustsImageWhenHighlighted = false;
ssBtn.selected = NO;
ssBtn.frame = CGRectMake(30, 100, 100, 30);
[self.view addSubview:ssBtn];
lrBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[lrBtn setTitle:@"Reset" forState:UIControlStateNormal];
[lrBtn addTarget:self action:@selector(resetOrLapTimer) forControlEvents:UIControlEventTouchUpInside];
lrBtn.adjustsImageWhenHighlighted = false;
lrBtn.enabled = false;
lrBtn.frame = CGRectMake(320-130, 100, 100, 30);
[self.view addSubview:lrBtn];
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(20, 150, 280, 300)];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
- (void)startOrStopTimer
{
if (ssBtn.selected) {
if (timer) {
[timer invalidate];
}
// lab.text = @" 00:00.0 ";
// timeCount = 0.0f;
// lapTimeCount = 0.0f;
[lrBtn setTitle:@"Reset" forState:UIControlStateNormal];
}else{
timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
[lrBtn setTitle:@"Lap" forState:UIControlStateNormal];
lrBtn.enabled = YES;
}
ssBtn.selected = !ssBtn.selected;
}
- (void)resetOrLapTimer
{
if (ssBtn.selected){
//lap
[dataArr insertObject:[NSString stringWithFormat:@" %02d:%04.1f ",(int)(lapTimeCount/60),lapTimeCount-(60*(int)(lapTimeCount/60))] atIndex:0];
lapTimeCount = 0.0f;
[_tableView reloadData];
}else{
lab.text = @" 00:00.0 ";
timeCount = 0.0f;
lapTimeCount = 0.0f;
[lrBtn setTitle:@"Reset" forState:UIControlStateNormal];
lrBtn.enabled = NO;
[dataArr removeAllObjects];
[_tableView reloadData];
}
}
- (void)updateTimer
{
timeCount += 0.1f;
lapTimeCount += 0.1f;
lab.text = [NSString stringWithFormat:@" %02d:%04.1f ",(int)(timeCount/60),timeCount-(60*(int)(timeCount/60))];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArr 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 = [dataArr objectAtIndex:row];
cell.detailTextLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:20.0];
cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
NSString *text = [NSString stringWithFormat:@"lap %d", [dataArr count]-row];
cell.textLabel.text = text;
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end