秒表功能实现

来源:互联网 发布:重庆软件学校 编辑:程序博客网 时间:2024/05/20 10:23

      秒表要实现的功能描述:下方灰色背景的view上有两个按钮-开始/停止按钮 计次按钮。点击开始按钮,中间的大时钟开始计时,这时点击计次按钮,右上角的小时钟会把此刻的瞬时时间记录下来,同时在下面的tableView上也会显示。点击停止按钮会把右上角的小时钟,中间的大时钟清零,同时,下面的tableView也会清空。


效果图如下:


      


具体代码实现:



#import "MiaoBiaoViewController.h"


#define kW self.view.frame.size.width

#define kH self.view.frame.size.height

int count=0;


@interface MiaoBiaoViewController ()


{

   NSTimer * _timer;  //定时器

   NSInteger _seconds;

}


//开始暂停按钮

@property (nonatomic,weak)UIButton * ssButton;


//计次按钮

@property (nonatomic,weak)UIButton * jcButton;


//右上角计次时间

@property (nonatomic,weak)UILabel * conLabel;


//中间秒表

@property (nonatomic,weak)UILabel * ctLabel;


//下面的每次记录的时间

@property (nonatomic,weak)UITableView * tableView;


//

@property (nonatomic,weak)UITableViewCell * cell;


//存放记录的数组

@property (nonatomic,strong)NSMutableArray * jcArray;


@end


@implementation MiaoBiaoViewController


#pragma mark - 懒加载

- (NSMutableArray *)jcArray

{

   if (_jcArray==nil)

    {

        _jcArray=[NSMutableArrayarray];

    }

    return _jcArray;

}


#pragma mark - 入口

- (void)viewDidLoad {

    [superviewDidLoad];

    

    [self_loadViews];

}


- (void) _loadViews

{

   self.title=@"秒表";

    //小时钟---一直计时

   UILabel * conLabel=[[UILabelalloc]initWithFrame:CGRectMake(267,65, 110, 50)];

    //conLabel.backgroundColor=[UIColor redColor];

    conLabel.text=@"00:00.00";

    conLabel.font=[UIFontfontWithName:nilsize:25];

   self.conLabel=conLabel;

    [self.viewaddSubview:conLabel];

    

   //秒表

    UILabel * ctLabel=[[UILabelalloc]initWithFrame:CGRectMake(0,110,kW,150)];

    //ctLabel.backgroundColor=[UIColor redColor];

    ctLabel.text=@"00:00.00";

    ctLabel.textAlignment=NSTextAlignmentCenter;

    ctLabel.font=[UIFontfontWithName:nilsize:75];

   self.ctLabel=ctLabel;

    [self.viewaddSubview:ctLabel];

    

    //下方视图

    UIView * bView=[[UIViewalloc]initWithFrame:CGRectMake(0,230,kW,140)];

    bView.backgroundColor=[UIColorcolorWithRed:0.1green:0.1blue:0.1alpha:0.1];

    

    [self.viewaddSubview:bView];

    

    //NSLog(@"%f",bView.frame.origin.y);

    

    //开始停止按钮

   UIButton * ssButton=[[UIButtonalloc]initWithFrame:CGRectMake((kW-200)/3,20, 100, 100)];

    ssButton.backgroundColor=[UIColorwhiteColor];

    ssButton.layer.cornerRadius=50;

    [ssButton setTitle:@"开始"forState:UIControlStateNormal];

    [ssButton setTitle:@"停止"forState:UIControlStateSelected];

    [ssButton setTitleColor:[UIColorredColor] forState:UIControlStateNormal];

    [ssButton setTitleColor:[UIColorgrayColor] forState:UIControlStateSelected];

    

    ssButton.tag=1;

    [ssButton addTarget:selfaction:@selector(StartStop:)forControlEvents:UIControlEventTouchUpInside];

   self.ssButton=ssButton;

    [bViewaddSubview:ssButton];

    

    

    //计次按钮

   UIButton * jcButton=[[UIButtonalloc]initWithFrame:CGRectMake(((kW-200)/3)*2+100,20, 100, 100)];

    jcButton.backgroundColor=[UIColorwhiteColor];

    jcButton.layer.cornerRadius=50;

    [jcButton setTitle:@"计次"forState:UIControlStateNormal];

    [jcButton setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    [jcButton addTarget:selfaction:@selector(CountNum)forControlEvents:UIControlEventTouchUpInside];

   self.jcButton=jcButton;

    [bViewaddSubview:jcButton];

    

    //点击计次按钮时记录的每次时间,存放到对应的cell

    UITableView * tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,370, kW, kH-370-60)style:UITableViewStylePlain];

    tableView.rowHeight=50;

    tableView.delegate=self;

    tableView.dataSource=self;

   self.tableView=tableView;

    [self.viewaddSubview:tableView];

}



#pragma mark - ssButton按钮的点击事件

- (void)StartStop:(UIButton *) button

{

    button.selected = !button.selected;

   if(_timer==nil)

    {

        //每隔0.01秒刷新一次页面

        _timer=[NSTimerscheduledTimerWithTimeInterval:0.01target:selfselector:@selector(runAction)userInfo:nilrepeats:YES];

        [[NSRunLoopcurrentRunLoop] addTimer:_timerforMode:NSRunLoopCommonModes];

        NSLog(@"开始计时.....");

    }

   else

    {

        [_timerinvalidate];   //让定时器失效

       _timer=nil;

       _ctLabel.text=@"00:00.00";

       _conLabel.text=@"00:00.00";

       _seconds=0;

       self.cell=nil;

       self.jcArray=nil;

        [self.tableViewreloadData];

        NSLog(@"计时停止.....");

    }

    

    //方法二

   /*

//    if (button.selected==1)

//    {

//        NSLog(@"开始计时.....");

//       

//    }

//    else

//    {

//        NSLog(@"计时停止.....");

//      

//    }

     */

    

}



#pragma mark - runAction

- (void) runAction

{

    _seconds++;

    //动态改变开始时间

   NSString * startTime=[NSStringstringWithFormat:@"%02li:%02li.%02li",_seconds/100/60%60,_seconds/100%60,_seconds%100];

   _ctLabel.text=startTime;

    

}


#pragma mark - 计次

- (void)CountNum

{

   count++;

    _conLabel.text=_ctLabel.text;

   // NSLog(@"这是记录的第**** %i ****个时间数据: %@",count,_conLabel.text);

    [self.jcArrayaddObject:_conLabel.text];

//    NSLog(@"%@",self.jcArray);

    

    [self.tableViewreloadData];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.jcArray.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

   static NSString * identy=@"JRTable";

    UITableViewCell * cell=[tableViewdequeueReusableCellWithIdentifier:identy];

   if (cell==nil)

    {

        cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identy];

        //增加label

//        UILabel * cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, kW, tableView.rowHeight)];

//        cellLabel.text=self.jcArray[indexPath.row];

//        cellLabel.textAlignment=NSTextAlignmentCenter;

//        [cell.contentView addSubview:cellLabel];

    }

     //NSLog(@"%@",self.jcArray[indexPath.row]);

    cell.textLabel.text =self.jcArray[indexPath.row];

    cell.textLabel.textAlignment=NSTextAlignmentCenter;

   self.cell=cell;

    return  self.cell;

}


@end


    PS:这里面有很多测试代码,且看且珍惜吧~敲代码的时候喜欢写很多注释,据说专业的程序员都很少写注释,真的吗?快被自己蠢哭了~ 明天周末啊~ 

0 1
原创粉丝点击