一个iOS上的秒表小应用的实现方法分享
程序员文章站
2022-05-16 11:35:03
模仿实现一下ios系统应用时钟里的秒表程序,就是这个应用:
主要实现的功能:
1.由start/stop键实现计时
2.有reset/lap键实现复位和计次...
模仿实现一下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 {
#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;
}
待改进的地方:
1.对于时间的计时操作和ui事件应该分不同线程实现,这里我偷懒了
2.对于时间的表示方法其实也是很偷懒的,没有按照标准的秒分进位表示
上一篇: JavaScript多种页面刷新方法小结
下一篇: 超实用教程:字体设计常用套路移花接木