您的位置:首页 > 移动开发 > IOS开发

iOS程序入门开发练习:秒表stopWatch

2013-09-23 10:30 260 查看
看了念茜的博客,按她的思路自己也动手写了一个。感觉她的代码里有些东西是没有必要的,并对她的逻辑进行了小的修改:start为YES时时间转,为NO时停止。这样比较符合我的思维习惯,

另外在创建NSTimer那里,使用scheduledTimerWithTimeInterval函数是创建一个NSTimer并把它放进了默认的runloop里,此runloop的模式是默认的NSDefaultRunLoopMode,应该将默认模式改成NSRunLoopCommonModes,不然在程序跑起来的时候拖拽tableView会卡住。其中的细节与原理还有待深究

下面是.m的代码:

#import "firstfbAppViewController.h"

@interface firstfbAppViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSString *timeStr;
BOOL start; //记录当前状态
NSTimer *timer;
float secCount;
NSInteger lapCount;
NSArray *timeList;
}
@property (strong, nonatomic) IBOutlet UILabel *timeShower;
@property (strong, nonatomic) IBOutlet UIButton *startOrStop;
@property (strong, nonatomic) IBOutlet UIButton *resetOrLap;
@property (strong, nonatomic) UITableView *lapTable;
@end

@implementation firstfbAppViewController
@synthesize timeShower = _timeShower;
@synthesize startOrStop = _startOrStop;
@synthesize resetOrLap = _resetOrLap;
@synthesize lapTable = _lapTable;

- (void)viewDidLoad
{
[super viewDidLoad];
[self.timeShower setText: @"00:00.0"];
[self.startOrStop setTitle:@"Start" forState:UIControlStateNormal];
[self.resetOrLap setTitle:@"Reset" forState:UIControlStateNormal];

CGRect screenRect = [[UIScreen mainScreen]bounds];
CGRect lapTableRect = CGRectMake(screenRect.origin.x, screenRect.size.height/2 - 30, screenRect.size.width, screenRect.size.height/2 + 30);
self.lapTable = [[UITableView alloc]initWithFrame:lapTableRect style:UITableViewStyleGrouped];

[self.lapTable setDelegate:self];
[self.lapTable setDataSource:self];
[self.view addSubview:self.lapTable];

start = NO;
secCount = lapCount = 0;
timer = [[NSTimer alloc]init];

}
- (IBAction)startOrStopClick
{
start = !start;
if (start)
{
[self.startOrStop setTitle:@"Stop" forState:UIControlStateNormal];
[self.resetOrLap setTitle:@"Lap" forState:UIControlStateNormal];
//timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; 就是这里需要注意,也可以使用这一行代码并用后面两句设置currentRunLoop的模式。
timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
NSRunLoop *t = [NSRunLoop currentRunLoop];
[t addTimer:timer forMode:NSRunLoopCommonModes];//这里
}
else
{
[self.startOrStop setTitle:@"Start" forState:UIControlStateNormal];
[self.resetOrLap setTitle:@"Reset" forState:UIControlStateNormal];
[timer invalidate];
}
}

- (IBAction)resetOrLap:(id)sender
{
if (start)  //lap
{
if (timeList == nil)
{
timeList = [[NSMutableArray alloc]initWithObjects:timeStr, nil];
}
else
{
timeList = [timeList arrayByAddingObject:timeStr];
//NSLog(@"timeListCount: %d",[timeList count]);
}
[self.lapTable reloadData];
}
else    //reset
{
timeStr = @"00:00.0";
self.timeShower.text = timeStr;
timeList = nil;
secCount = 0;
[self.lapTable reloadData];
}
}

- (void)updateTime
{
secCount += 0.1;
timeStr = [NSString stringWithFormat:@"%02d:%04.1f",(int)(secCount/60),secCount - 60 * (int)(secCount/60)];
self.timeShower.text = timeStr;
NSLog(@"timeStr: %@",timeStr);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [timeList count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableViewIdentifier = @"tableViewIdentifier";
UITableViewCell *timeCell = [tableView dequeueReusableCellWithIdentifier:tableViewIdentifier];
if (timeCell == nil)
{
timeCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:tableViewIdentifier];
}
NSUInteger timeRow = [indexPath row];
//NSLog(@"timeRow: %d",timeRow);

timeCell.detailTextLabel.text = [timeList objectAtIndex:timeRow];
timeCell.detailTextLabel.textColor = [UIColor blackColor];
timeCell.detailTextLabel.font = [UIFont boldSystemFontOfSize:25.0];
timeCell.detailTextLabel.textAlignment = UITextAlignmentCenter;

NSString *text = [[NSString alloc]initWithFormat:@"lap %d",timeRow + 1];
timeCell.textLabel.text = text;
return timeCell;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: