iOS程序入门开发练习:秒表stopWatch
2013-09-23 10:30
260 查看
看了念茜的博客,按她的思路自己也动手写了一个。感觉她的代码里有些东西是没有必要的,并对她的逻辑进行了小的修改:start为YES时时间转,为NO时停止。这样比较符合我的思维习惯,
另外在创建NSTimer那里,使用scheduledTimerWithTimeInterval函数是创建一个NSTimer并把它放进了默认的runloop里,此runloop的模式是默认的NSDefaultRunLoopMode,应该将默认模式改成NSRunLoopCommonModes,不然在程序跑起来的时候拖拽tableView会卡住。其中的细节与原理还有待深究
下面是.m的代码:
另外在创建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
相关文章推荐
- iOS开发练习小程序——秒表
- iOS开发练习小程序——秒表
- iOS入门开发计算器小程序,包括button、label、image的使用
- iOS从入门到精通---1.iOS开发App程序启动原理
- iOS开发-Xcode入门ObjC程序
- ios开发入门-我的第一个ios程序 helloword
- iOS蓝牙开发入门05--模仿demo写程序
- 01_iOS开发入门_UI程序的执行流程
- IOS开发入门----UIApplication和AppDelegate以及IOS程序启动的过程(2017/8/8)
- ios开发入门-我的第一个ios程序 helloword
- iOS开发 入门01-第一个iOS程序
- 小程序开发入门
- iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
- [入门九]ADT和Eclipse开发Android程序时注意点
- lae界面开发工具入门之介绍十二--<iOS系统如何编译打包?>
- 用Keil-MDK开发TQ2440裸机程序入门教程——LED流水灯实现
- iOS开发那些事-如何编译iOS程序
- nokey属性(输出没有名称的数组)---精通android、IOS App应用服务程序开发
- JavaBeans程序开发从入门到精通
- 尚学堂IOS开发入门到精通[4个月IOS实体教程]