I am not busy with my recent work. In my spare time, I learned how to use object-C to experience an invincible IOS framework. I want to write a small application trainer and pick a simple question for myself: imitating and implementing the stopwatch program in the IOS system application clock is the application:
Main functions:
1. Timing by the start/stop key
2. Reset and count with the reset/lap key
Considerations:
1. Time Representation (there are many ideas)
2. Sort the count data in reverse order, that is, the count 1 data is at the bottom, and the count 2 data is in the descending order, and the count 3 time data is in the descending order.
My implementation:
Arc saves most of our memory management tasks and is comfortable with C ++.
-(Ibaction) startorstop :( uibutton *) sender {// click the switch button. The background image * 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 = [nst1_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 duration: 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 identifier: 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 ;}
:
To be improved:
1. Timing operations and UI events should be implemented in different threads. I am lazy here.
2. The representation of time is actually very lazy, not represented by the standard second-point.
If you find any errors or errors, please kindly advise. Thank you.