Stopwatch function Realization

Source: Internet
Author: User

Stopwatch to achieve the function description: The gray background below the view has two buttons-start/Stop button and the Count button. Click on the Start button, the middle of the big clock start time, then click the Count button, the upper right corner of the small clock will be the moment of the instantaneous times recorded, but also on the following tableview will be displayed. Clicking the Stop button will clear the small clock in the upper right corner, the large clock in the middle, and the tableview below will also be emptied.


The effect diagram is as follows:




Specific code implementation:



#import "MiaoBiaoViewController.h"


#define KW Self.view.frame.size.width

#define KH Self.view.frame.size.height

int count=0;


@interface Miaobiaoviewcontroller ()


{

Nstimer * _timer; Timer

Nsinteger _seconds;

}


Start Pause button

@property (nonatomic,weak) UIButton * Ssbutton;


Time-Count button

@property (nonatomic,weak) UIButton * Jcbutton;


upper right corner count time

@property (nonatomic,weak) UILabel * Conlabel;


Intermediate stopwatch

@property (nonatomic,weak) UILabel * Ctlabel;


The time of each record below

@property (nonatomic,weak) UITableView * TableView;


//

@property (nonatomic,weak) UITableViewCell * cell;


Array for storing records

@property (Nonatomic,strong) nsmutablearray * Jcarray;


@end


@implementation Miaobiaoviewcontroller


#pragma mark-Lazy loading

-(Nsmutablearray *) Jcarray

{

if (_jcarray==nil)

{

_jcarray=[nsmutablearray array];

}

return _jcarray;

}


#pragma mark-Entrance

-(void) Viewdidload {

[Super Viewdidload];

[Self _loadviews];

}


-(void) _loadviews

{

self.title=@ "Stopwatch";

The small clock---kept ticking.

UILabel * Conlabel=[[uilabel alloc]initwithframe:cgrectmake (267, 65, 110, 50)];

Conlabel.backgroundcolor=[uicolor Redcolor];

conlabel.text=@ "00:00.00";

Conlabel.font=[uifont Fontwithname:nil size:25];

Self.conlabel=conlabel;

[Self.view Addsubview:conlabel];

Stopwatch

UILabel * Ctlabel=[[uilabel Alloc]initwithframe:cgrectmake (0,110,kw,150)];

Ctlabel.backgroundcolor=[uicolor Redcolor];

ctlabel.text=@ "00:00.00";

Ctlabel.textalignment=nstextalignmentcenter;

Ctlabel.font=[uifont Fontwithname:nil size:75];

Self.ctlabel=ctlabel;

[Self.view Addsubview:ctlabel];

View below

UIView * Bview=[[uiview Alloc]initwithframe:cgrectmake (0,230,kw,140)];

Bview.backgroundcolor=[uicolor colorwithred:0.1 green:0.1 blue:0.1 alpha:0.1];

[Self.view Addsubview:bview];

NSLog (@ "%f", BVIEW.FRAME.ORIGIN.Y);

Start Stop button

UIButton * Ssbutton=[[uibutton Alloc]initwithframe:cgrectmake ((kW-200)/3, 20, 100, 100)];

Ssbutton.backgroundcolor=[uicolor Whitecolor];

ssbutton.layer.cornerradius=50;

[Ssbutton settitle:@ "Start" forstate:uicontrolstatenormal];

[Ssbutton settitle:@ "Stop" forstate:uicontrolstateselected];

[Ssbutton Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];

[Ssbutton Settitlecolor:[uicolor Graycolor] forstate:uicontrolstateselected];

ssbutton.tag=1;

[Ssbutton addtarget:self Action: @selector (startstop:) forcontrolevents:uicontroleventtouchupinside];

Self.ssbutton=ssbutton;

[Bview Addsubview:ssbutton];

Time-Count button

UIButton * Jcbutton=[[uibutton Alloc]initwithframe:cgrectmake (((kW-200)/3) *2+100, 20, 100, 100)];

Jcbutton.backgroundcolor=[uicolor Whitecolor];

jcbutton.layer.cornerradius=50;

[Jcbutton settitle:@] "forstate:uicontrolstatenormal";

[Jcbutton Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];

[Jcbutton addtarget:self Action: @selector (Countnum) forcontrolevents:uicontroleventtouchupinside];

Self.jcbutton=jcbutton;

[Bview Addsubview:jcbutton];

Each time recorded when clicking on the Count button, stored on the corresponding cell

UITableView * Tableview=[[uitableview alloc]initwithframe:cgrectmake (0, 370, KW, kh-370-60) style: Uitableviewstyleplain];

tableview.rowheight=50;

tableview.delegate=self;

tableview.datasource=self;

Self.tableview=tableview;

[Self.view Addsubview:tableview];

}



#pragma click events for Mark-ssbutton buttons

-(void) Startstop: (UIButton *) button

{

button.selected =!button.selected;

if (_timer==nil)

{

Refresh the page every 0.01 seconds

_timer=[

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.