• ioc开发学习 --简易计时器 (基于iPhone5屏幕尺寸开发)


    超简单的秒表:包含:开始、暂停(不清零)、清零 方法

    核心代码

    //
    //  ViewController.m
    //  MiaoBiao
    //
    //  Created by Ibokan on 15/8/18.
    //  Copyright (c) 2015年 Crazy凡. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    {
        int _alltime;
    }
    @property(nonatomic,strong)UIColor * EnabledColor;//定义全局可按按钮颜色
    @property(nonatomic,strong)UILabel *label;//时间显示label
    @property(nonatomic,strong)UIButton *buttonStart;//开始按钮
    @property(nonatomic,strong)UIButton *buttonPause;//暂停按钮
    @property(nonatomic,strong)UIButton *buttonClear;//清零
    @property(nonatomic,strong)NSTimer *timer;//计时器
    @property(nonatomic,strong)UILabel *labelinfo;//作者信息
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        _EnabledColor = [UIColor colorWithRed:20/255.0 green:198/255.0 blue:233/255.0 alpha:1]; //初始化全局颜色
        
        
        //初始换显示label
        self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
        self.label.backgroundColor = _EnabledColor;
        self.label.text = @"00:00:00";
        self.label.font = [UIFont fontWithName:@"Helvetica" size:50];
        self.label.textColor = [UIColor whiteColor];
        self.label.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:self.label];
        
        //初始化开始按钮
        self.buttonStart = [[UIButton alloc]initWithFrame:CGRectMake(0, 210, 155, 150)];
        [self.buttonStart setTitle:@"Start" forState:UIControlStateNormal];
        self.buttonStart.backgroundColor = _EnabledColor;
        [self.buttonStart setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.view addSubview:self.buttonStart];
        [self.buttonStart addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
        
        //初始化暂停按钮
        self.buttonPause = [[UIButton alloc]initWithFrame:CGRectMake(165, 210, 155, 150)];
        [self.buttonPause setTitle:@"Pause" forState:UIControlStateNormal];
        self.buttonPause.backgroundColor = _EnabledColor;
        [self.buttonPause setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.view addSubview:self.buttonPause];
        [self.buttonPause addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];
    
        //初始化清除按钮
        self.buttonClear = [[UIButton alloc]initWithFrame:CGRectMake(0, 370, 320, 150)];
        [self.buttonClear setTitle:@"Clear" forState:UIControlStateNormal];
        self.buttonClear.backgroundColor = _EnabledColor;
        [self.buttonClear setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.view addSubview:self.buttonClear];
        [self.buttonClear addTarget:self action:@selector(clear) forControlEvents:UIControlEventTouchUpInside];
    
        //初始化信息显示label
        self.labelinfo = [[UILabel alloc]initWithFrame:CGRectMake(0, 530, 320, 40)];
        self.labelinfo.backgroundColor = _EnabledColor;
        self.labelinfo.text = @"Developed by Crazy凡";
        self.labelinfo.textColor = [UIColor whiteColor];
        self.labelinfo.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:self.labelinfo];
        
        [self clear];//调用clear方法为组件初始化
    }
    
    - (void)start
    {
       
        [self.buttonStart setTitle:@"Start" forState:UIControlStateNormal];//改变开始按钮显示字符(为暂停后初始化做准备)
        [self startUnenabled];
        [self pauseEnabled];
        [self clearEnabled];
        
         //timer 初始化 哪里需要哪里调用,
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(add) userInfo:nil repeats:YES];
    
    
    }
    
    - (void)pause
    {
        [self.buttonStart setTitle:@"Continue" forState:UIControlStateNormal];//改变开始按钮显示字符
        [self.timer invalidate];
        [self startEnabled];
        [self pauseUnenabled];
    }
    
    - (void)clear
    {
        _alltime = 0;
    //    _millisecond = 0;
    //    _second = 0;
    //    _minute = 0;
        [self.timer invalidate];
        self.label.text = @"00:00:00";
        [self.buttonStart setTitle:@"Start" forState:UIControlStateNormal];
        [self startEnabled];
        [self pauseUnenabled];
        [self clearUnenabled];
    }
    - (void)add
    {
        _alltime++;
        self.label.text = [[NSString alloc]initWithFormat:@"%02d:%02d:%02d",_alltime/6000,_alltime%6000/100,_alltime%100];
        //self.label.text = [[[NSString alloc]initWithFormat:@"%02d:%02d:%02d",_alltime/6000,_alltime%6000/100,_alltime%100] stringByAppendingString:[[NSString alloc]initWithFormat:@"%@",[[NSDate alloc]init] ]];
        
    }
    
    //以下是按钮可用性控制方法
    - (void)startEnabled
    {
        self.buttonStart.enabled = true;
        self.buttonStart.backgroundColor = _EnabledColor;
    }
    - (void)startUnenabled
    {
        self.buttonStart.enabled = false;
        self.buttonStart.backgroundColor = [UIColor darkGrayColor];
    
    }
    - (void)pauseEnabled
    {
        self.buttonPause.enabled = true;
        self.buttonPause.backgroundColor = _EnabledColor;
    }
    - (void)pauseUnenabled
    {
        self.buttonPause.enabled = false;
        self.buttonPause.backgroundColor = [UIColor darkGrayColor];
    }
    - (void)clearEnabled
    {
        self.buttonClear.enabled = true;
        self.buttonClear.backgroundColor = _EnabledColor;
    }
    - (void)clearUnenabled
    {
        self.buttonClear.enabled = false;
        self.buttonClear.backgroundColor = [UIColor darkGrayColor];
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    View Code

    学习点

    1、[UIColor colorWithRed:20/255.0 green:198/255.0 blue:233/255.0 alpha:1]; 

    //OC 之自定义颜色

    2、self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(add) userInfo:nil repeats:YES];

    //NSTimer 使用方法

    3、[self.buttonStart addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];

    //回调方法

    注意:以下为重新编辑部分:源码以及下面可以下载的源码中不包含此部分

    4、  NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 

      [formatter setDateFormat:@"mm:ss:SS"]; //

      NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:_alltime];

      NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];

      self.label.text = confromTimespStr;

    //格式化时间输出

    用以上代码替换源码中的:self.label.text = [[NSString alloc]initWithFormat:@"%02d:%02d:%02d",_alltime/6000,_alltime%6000/100,_alltime%100]; 一句

    此处粗讲一下时间格式化:

    把一行代码可以搞定的事情替换成一堆代码,上面的替换代码看似更麻烦了,这样做并非没有意;或许在此处显得找麻烦,但是当需要转换的时间值过大时,手动转换就会显得尤为尴尬,因为需要考虑各种因素(闰年、闰秒之类的);

    系统提供的方法可以直接将数字转换成时间格式;

    原理是:

      [NSDate dateWithTimeIntervalSince1970:_alltime]会把后面的_alltime这个数字表示的时间(s)转换成自1970年1月1日0时0分0秒0毫秒到现在的时间差(时间格式的),至于为什么是1970年的,这里不多讲;现在的计算机和一些电子设备的时间是以从当前到1970年1月1日0时0分0秒0毫秒的偏移量(ms)为标准的;这里使用dateWithTimeIntervalSince1970方法,就会把原来额double类型的数据转换成时间类型,格式由“[formatter setDateFormat:@"mm:ss:SS"]”决定;后面我会列出时间的格式化关键字;

     

    编号

    含义

    备注

    备注

    1

    ddd

    日(101

    2

    EEEEE

    daysunsunday

    3

    MMMMMMMMMM

    月(101JanJanuary

    4

    gg

    显示时代、纪元

    5

    hhh

    小时(101

    12小时制

    6

    HHH

    小时(101

    24小时制

    7

    mmm

    分钟(101

    8

    sss

    秒(101

    9

    SSSSSS

    毫秒(1位、2位、3位)

    10

    yyyyyyyyyy

    年份

    这里是个粗略的,详细可以去看看http://www.cnblogs.com/Cristen/p/3599922.html  不过其中的ff 表示并没有测试出来。

    此处编辑于:2015-08-20

     点我下载源码

  • 相关阅读:
    Vagrant 扩大磁盘根目录
    阿里云 轻量应用服务器 vnc 远程桌面连接
    图片加水印C#源代码
    Asp.net网站Pdf加水印C#源代码
    [FAQ] uni-app 如何让页面不展示返回箭头图标
    [PHP] composer, PHP Fatal error: Allowed memory size of xx bytes exhausted
    [FE] uni-app 导航栏开发指南
    [FE] uni-app 动态改变 navigationBarTitleText 导航标题
    [FE] yarn, npm 切换镜像源
    [FAQ] Phpstorm 代码提示功能失效问题
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/4743226.html
Copyright © 2020-2023  润新知