zoukankan      html  css  js  c++  java
  • 秒表功能实现

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

    点击停止button会把右上角的小时钟,中间的大时钟清零。同一时候,以下的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;

    }


    //開始暂停button

    @property (nonatomic,weak) UIButton * ssButton;


    //计次button

    @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=[NSMutableArray array];

        }

        return  _jcArray;

    }


    #pragma mark - 入口

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        [self _loadViews];

    }


    - (void) _loadViews

    {

        self.title=@"秒表";

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

        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];

        

        //秒表

        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];

        

        //下方视图

        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);

        

        //開始停止button

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

        ssButton.backgroundColor=[UIColor whiteColor];

        ssButton.layer.cornerRadius=50;

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

        [ssButton setTitle:@"停止" 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];

        

        

        //计次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];

        

        //点击计次button时记录的每次时间,存放到相应的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 mark - ssButtonbutton的点击事件

    - (void)StartStop:(UIButton *) button

    {

        button.selected = !button.selected;

        if(_timer==nil)

        {

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

            _timer=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(runAction) userInfo:nil repeats:YES];

            [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];

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

        }

        else

        {

            [_timer invalidate];   //让定时器失效

            _timer=nil;

            _ctLabel.text=@"00:00.00";

            _conLabel.text=@"00:00.00";

            _seconds=0;

            self.cell=nil;

            self.jcArray=nil;

            [self.tableView reloadData];

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

        }

        

        //方法二

        /*

    //    if (button.selected==1)

    //    {

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

    //       

    //    }

    //    else

    //    {

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

    //      

    //    }

         */

        

    }



    #pragma mark - runAction

    - (void) runAction

    {

        _seconds++;

        //动态改变開始时间

        NSString * startTime=[NSString stringWithFormat:@"%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.jcArray addObject:_conLabel.text];

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

        

        [self.tableView reloadData];

    }


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

    {

        return self.jcArray.count;

    }


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

    {

        static NSString * identy=@"JRTable";

        UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identy];

        if (cell==nil)

        {

            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: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:这里面有非常多測试代码。且看且珍惜吧~ 写程序的时候喜欢写非常多凝视,据说专业的程序猿都非常少写凝视,真的吗?快被自己蠢哭了~ 明天周末啊~ 

  • 相关阅读:
    一点一点学习GIt
    17-ajax向后端提交POST请求
    16-djongo中间件学习
    ES6快速入门
    15 Django组件——中间件
    导入Scrapy 项目报错:no module named scrapy
    slf4j + logback 输出日志:mybatis sql语句
    java实现链表
    Log4j2.xml的日志设置
    java多线程之生产者消费者经典问题
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6939966.html
Copyright © 2011-2022 走看看