A stopwatch on IOS implementation method for small application sharing _ios

Source: Internet
Author: User

Simulation to implement the iOS system application clock in the stopwatch program, is this application:

The main features of the implementation:
1. By the Start/stop key to achieve the timing
2. Have reset/lap key to realize reset and count time

The point to think about:
1. The presentation of time (there are many ways to train)
2. The reverse arrangement of the data, that is, 1 of the data at the bottom, in turn up to the time of 2, 3 of the Times data

My implementation:
ARC eliminates most of the things we manage in our own memory, and it's so much more comfortable to write in C + +

Copy Code code as follows:

-(Ibaction) Startorstop: (UIButton *) sender
{
Click the toggle button background image
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-(M * (int) (TIME/60))];
Timestr_lap = [NSString stringwithformat:@ "%02d:%04.1f", (int) (TIME_LAP/60), Time_lap-(M * (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;
}


Where to improve:
1. Timing operations and UI events for time should be implemented in different threads, I'm lazy here.
2. The presentation of time is also very lazy, not in accordance with the standard seconds of the decimal notation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.