一個(gè)iOS上的秒表小應(yīng)用的實(shí)現(xiàn)方法分享
模仿實(shí)現(xiàn)一下ios系統(tǒng)應(yīng)用時(shí)鐘里的秒表程序,就是這個(gè)應(yīng)用:

主要實(shí)現(xiàn)的功能:
1.由start/stop鍵實(shí)現(xiàn)計(jì)時(shí)
2.有reset/lap鍵實(shí)現(xiàn)復(fù)位和計(jì)次
需要思考的點(diǎn):
1.時(shí)間的表示方法(有很多種思路)
2.計(jì)次數(shù)據(jù)的倒序排列,即計(jì)次1的數(shù)據(jù)在最底端,依次向上為計(jì)次2,計(jì)次3的時(shí)間數(shù)據(jù)
我的實(shí)現(xiàn):
ARC省去了我們自行管理內(nèi)存的大部分事情,寫(xiě)慣了c++于是舒服了很多
- (IBAction) startOrstop:(UIButton *)sender
{
//點(diǎn)擊切換按鈕背景圖
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 {
#if 0
[list arrayByAddingObject:timestr];
#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];
#endif
}
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 = UITextAlignmentCenter;
NSString *text = [[NSString alloc]initWithFormat:@"lap %@", [list_lap objectAtIndex:row]];
cell.textLabel.text = text;
return cell;
}



待改進(jìn)的地方:
1.對(duì)于時(shí)間的計(jì)時(shí)操作和UI事件應(yīng)該分不同線程實(shí)現(xiàn),這里我偷懶了
2.對(duì)于時(shí)間的表示方法其實(shí)也是很偷懶的,沒(méi)有按照標(biāo)準(zhǔn)的秒分進(jìn)位表示
- javascript設(shè)計(jì)簡(jiǎn)單的秒表計(jì)時(shí)器
- js實(shí)現(xiàn)簡(jiǎn)單秒表走動(dòng)的時(shí)鐘特效
- 原生js 秒表實(shí)現(xiàn)代碼
- 魔方在線秒表javascript版
- Android自定義Chronometer實(shí)現(xiàn)短信驗(yàn)證碼秒表倒計(jì)時(shí)功能
- BOM系列第三篇之定時(shí)器應(yīng)用(時(shí)鐘、倒計(jì)時(shí)、秒表和鬧鐘)
- Android實(shí)現(xiàn)的秒表計(jì)時(shí)器示例
- js判斷手機(jī)端(Android手機(jī)還是iPhone手機(jī))
- javascript判斷iphone/android手機(jī)橫豎屏模式的函數(shù)
- js仿iphone秒表功能 計(jì)算平均數(shù)
相關(guān)文章
iOS App開(kāi)發(fā)中用CGContextRef繪制基本圖形的基本示例
這篇文章主要介紹了iOS App開(kāi)發(fā)中用CGContextRef繪制基本圖形的基本示例,CGContextRef同時(shí)可以進(jìn)行圖形顏色的填充以及文字的書(shū)寫(xiě),需要的朋友可以參考下2016-05-05
iOS11實(shí)現(xiàn)App內(nèi)自動(dòng)連接Wi-Fi的方法
這篇文章主要給大家介紹了關(guān)于iOS11實(shí)現(xiàn)App內(nèi)自動(dòng)連接Wi-Fi的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
iOS實(shí)現(xiàn)微信/QQ顯示最近拍攝圖片的功能實(shí)例代碼
如果你剛剛拍攝了圖片,在使用微信/QQ發(fā)生消息時(shí)會(huì)顯示“你可能要發(fā)送的圖片”,這個(gè)功能非常人性化,怎么實(shí)現(xiàn)的呢?下面小編給大家分享iOS實(shí)現(xiàn)微信/QQ顯示最近拍攝圖片的功能實(shí)例代碼,一起看看吧2017-03-03
iOS 本地視頻和網(wǎng)絡(luò)視頻流播放實(shí)例代碼
本篇文章主要介紹了iOS 本地視頻和網(wǎng)絡(luò)視頻流播放實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
iOS中containsString和rangeOfString的區(qū)別小結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于iOS中containsString和rangeOfString的一些區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01

