将时间对象的字符串中的时间部分截取出来, 简单的秒表定时器

和数字转换成字符串来进行秒表的设计,通过字符串转换成数字,和数字转换成字符串来进行秒表的设计,通过字符串转换成数字,和数字转换成字符串来进行秒表的设计,通过字符串转换成数字,分别用于实现与时间相关的四个应用,  最简单的时钟制作办法是通过,  简易倒计时就是每1s通过setInterval将设置的时间减去1来达到要求,【1】简易倒计时

粗略的秒表反应计时器,轻易秒表电磁照望计时器

 轻便的秒表计时器

思路:

1.分头加多秒表中的上一些,样式如 00 :00 : 00 .
00,分别代表时,分,秒,皮秒一共用7个labe完毕

2.安装开关,分别是始于,甘休,重新初始化

3,通过字符串转变来数字,和数字调换来字符串来拓展秒表的准备

 

  1 #import "ViewController.h"
  2 
  3 #import "UIView+FrameExtension.h"
  4 
  7 #define kDeviceWidth [UIScreen mainScreen].bounds.size.width
  8 
  9 #define kDeviceHeight [UIScreen mainScreen].bounds.size.height
 10 
 13 @interface ViewController (){
 14 
 15     UILabel *_lbl1;
 16 
 17     UILabel *_lbl2;
 18 
 19     UILabel *_lbl3;
 20 
 21     UILabel *_lbl4;

 25     NSTimer *_timer;
 26 
 27     BOOL    _isRunning;
 28 
 29 }
 33 @end
 34 
 37 @implementation ViewController
 38 
 41 - (void)viewDidLoad {
 42 
 43     [super viewDidLoad];
 44 
 47     [self createLabel];     //创建7个标签
 48 
 49     [self createTimer];     //创建1个定时器
 50 
 51     [self createButton];    //创建3个按
 52 
 53 }
 54 
 57 -(void)createLabel{

 61     UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(90, 200, 30, 40)];
 62 
 63     lbl1.text = @"00";
 64 
 65     lbl1.textColor = [UIColor redColor];
 66 
 67     [self.view addSubview:lbl1];
 68 
 69     _lbl1 = lbl1;
 70 
 73     UILabel *lbl11 = [[UILabel alloc]initWithFrame:CGRectMake(lbl1.right, lbl1.y, 10, lbl1.height)];
 74 
 75     lbl11.text = @":";
 76 
 77     lbl11.textColor = [UIColor redColor];
 78 
 79     [self.view addSubview:lbl11];
 80 
 83      UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(lbl11.right, lbl1.y, lbl1.width, lbl1.height)];
 84 
 85     lbl2.text = @"00";
 86 
 87     lbl2.textColor = [UIColor redColor];
 88 
 89     [self.view addSubview:lbl2];
 90 
 91     _lbl2 = lbl2;
 92 
 95     UILabel *lbl22 = [[UILabel alloc]initWithFrame:CGRectMake(lbl2.right, lbl1.y, lbl11.width, lbl1.height)];
 96 
 97     lbl22.text = @":";
 98 
 99     lbl22.textColor = [UIColor redColor];
100 
101     [self.view addSubview:lbl22];
102 
103     UILabel *lbl3 = [[UILabel alloc]initWithFrame:CGRectMake(lbl22.right, lbl1.y, lbl1.width, lbl1.height)];
104 
105     lbl3.text = @"00";
106 
107     lbl3.textColor = [UIColor redColor];
108 
109     [self.view addSubview:lbl3];
110 
111     _lbl3 = lbl3;
112 
115     UILabel *lbl33 = [[UILabel alloc]initWithFrame:CGRectMake(lbl3.right, lbl1.y, lbl11.width, lbl1.height)];
116 
117     lbl33.text = @".";
118 
119     lbl33.textColor = [UIColor redColor];
120 
121     [self.view addSubview:lbl33]; 
122 
123     UILabel *lbl4 = [[UILabel alloc]initWithFrame:CGRectMake(lbl33.right, lbl1.y, lbl1.width, lbl1.height)];
124 
125     lbl4.text = @"00";
126 
127     lbl4.textColor = [UIColor redColor];
128 
129     [self.view addSubview:lbl4];
130 
131     _lbl4 = lbl4;
132 
137 }

141 -(void)createButton{
142  
146 
147     UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(70, _lbl1.bottom+20, 60, 40)];
148 
149     [btn1 setTitle:@"开始" forState:UIControlStateNormal];
150 
151     [self.view addSubview:btn1];
152 
153     [btn1 setBackgroundImage:[UIImage imageNamed:@"logoff_btn_s"] forState:UIControlStateNormal];
154 
155     [btn1 setBackgroundImage:[UIImage imageNamed:@"submit_discussion_n"] forState:UIControlStateHighlighted];
156 
157     [btn1 addTarget:self action:@selector(start ) forControlEvents:UIControlEventTouchUpInside];
158 
163     UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(btn1.right+10, btn1.y, btn1.width, btn1.height)];
164 
165     [btn2 setTitle:@"停止" forState:UIControlStateNormal];
166 
167     [self.view addSubview:btn2];
168 
169     [btn2 setBackgroundImage:[UIImage imageNamed:@"logoff_btn_s"] forState:UIControlStateNormal];
170 
171     [btn2 setBackgroundImage:[UIImage imageNamed:@"submit_discussion_n"] forState:UIControlStateHighlighted];
172 
173     [btn2 addTarget:self action:@selector(stop ) forControlEvents:UIControlEventTouchUpInside];
174 
179     UIButton *btn3 = [[UIButton alloc]initWithFrame:CGRectMake(btn2.right+10,btn1.y , btn1.width, btn1.height)];
180 
181     [btn3 setTitle:@"复位" forState:UIControlStateNormal];
182 
183     [self.view addSubview:btn3];
184 
185     [btn3 setBackgroundImage:[UIImage imageNamed:@"logoff_btn_s"] forState:UIControlStateNormal];
186 
187     [btn3 setBackgroundImage:[UIImage imageNamed:@"submit_discussion_n"] forState:UIControlStateHighlighted];
188 
189     [btn3 addTarget:self action:@selector(fuwei ) forControlEvents:UIControlEventTouchUpInside];
190 
191 }
192 
195 -(void)start{

199     [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
200 
201     _isRunning = YES;
202   
204 
205     if (_isRunning) {       //表示定时器正在运行
206 
207         [_timer setFireDate:[NSDate distantPast]];
208 
211     }else{
212 
213         [_timer setFireDate:[NSDate distantFuture]];
214 
215     }
216 
217 }
218 
221 -(void)stop{
222 
225     _isRunning = !_isRunning;
226 
229     if (_isRunning) {       // 表示定时器正在运行
230 
233     }else{
234 
235         [_timer setFireDate:[NSDate distantFuture]];
236 
237     }
238 
239     _isRunning = NO;
240 
241 }
242 
245 -(void)fuwei{
246 
251         NSString* str4 = _lbl4.text;
252 
253         NSString* str3 = _lbl3.text;
254 
255         NSString* str2 = _lbl2.text;
256 
257         NSString* str1 = _lbl1.text;
258 
261         int x4 = [str4 intValue];
262 
263         int x3 = [str3 intValue];
264 
265         int x2 = [str2 intValue];
266 
267         int x1 = [str1 intValue];
268 
271         x4 = x3 = x2 = x1 = 0;
272 
275         NSString* str44 = [NSString stringWithFormat:@"%02d",x4];
276 
277         NSString* str33 = [NSString stringWithFormat:@"%02d",x3];
278 
279         NSString* str22 = [NSString stringWithFormat:@"%02d",x2];
280 
281         NSString* str11 = [NSString stringWithFormat:@"%02d",x1];
282 
283         _lbl4.text = str44;
284 
285         _lbl3.text = str33;
286 
287         _lbl2.text = str22;
288 
289         _lbl1.text = str11;
290 
293     _isRunning = !_isRunning;
294 
297     if (_isRunning) {       // 表示定时器正在运行
298 

301     }else{
302 
303         [_timer setFireDate:[NSDate distantFuture]];
304 
305     }
306 
307     _isRunning = NO;
308 

311 }
312 

315 -(void)createTimer{

319     _timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(move ) userInfo:nil repeats:YES];
320 
323     _isRunning = NO;
324 
327     //     下面这种方法创建的定时器,会自动的加入运行循环
328 
329     //    _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(move) userInfo:nil repeats:YES];
330 
331 }
332 
334 
335 -(void)move{
336 
339      NSString* str4 = _lbl4.text;
340 
341      NSString* str3 = _lbl3.text;
342 
343      NSString* str2 = _lbl2.text;
344 
345      NSString* str1 = _lbl1.text;
346 
349     int x4 = [str4 intValue];
350 
351     int x3 = [str3 intValue];
352 
353     int x2 = [str2 intValue];
354 
355     int x1 = [str1 intValue];
356 
359     x4++;
360 
363     if (x4 == 100 ) {
364 
365         x3 = x3 + 1;
366 
367         x4 = 0;
368 
369         if (x3 == 60) {
370 
371             x2 = x2 + 1;
372 
373             x3 = 0;
374 
375             if (x2 == 60 ) {
376 
377                 x1 = x1 + 1;
378 
379                 x2 = 0;
380 
381                 if (x1 == 24 ) {
382 
383                     x4 = x3 = x2 = x1 = 0;
384 
385                 }
386 
387             }
388 
389         }
390 
391     }
392    
394 
395     NSString* str44 = [NSString stringWithFormat:@"%02d",x4];
396 
397     NSString* str33 = [NSString stringWithFormat:@"%02d",x3];
398 
399     NSString* str22 = [NSString stringWithFormat:@"%02d",x2];
400 
401     NSString* str11 = [NSString stringWithFormat:@"%02d",x1];
402 
403     _lbl4.text = str44;
404 
405     _lbl3.text = str33;
406 
407     _lbl2.text = str22;
408 
409     _lbl1.text = str11;   
410 
411 }
412 
413  @end


开始

图片 1

 

停止

图片 2

复位

图片 3

源文件在此间,希望可以帮到你:

 

简单的秒表计时器思路: 1.分别增加秒表中的上有个别,样式如 00 :00 : 00 .
00,分别表示时,分,秒…

 轻易的秒表电磁关照计时器

 简单的秒表计时器

眼下的话

  本文属于计时器的运用有的,分别用于落到实处与时间相关的八个利用,包涵时钟、倒计时、秒表和时钟。与时光相关必要用到时间和日期对象Date,详细的情况活动至此

 

时钟

思路:

思路:

时钟

  最简易的石英钟制作方法是因此正则表达式的exec()方法,将时间对象的字符串中的时间有些截抽取来,使用放大计时器刷新就可以

<div id="myDiv"></div>
<script>
myDiv.innerHTML = /\d\d:\d\d:\d\d/.exec(new Date().toString())[0];
setInterval(function(){
    myDiv.innerHTML = /\d\d:\d\d:\d\d/.exec(new Date().toString())[0];    
},500);
</script>

 

  最简便易行的时钟制作方式是因而正则表明式的exec()方法,将时间对象的字符串中的时间部分截抽出来,使用电磁打点计时器刷新就可以

1.分级加多秒表中的上有的,样式如 00 :00
: 00 . 00,分别表示时,分,秒,纳秒一共用7个labe完结

1.独家加多秒表中的上有个别,样式如 00 :00
: 00 . 00,分别代表时,分,秒,皮秒一共用7个labe落成

倒计时

【1】简易倒计时

  简易倒计时正是每1s经过setInterval将安装的日子减去1来落成供给

<div id="myDiv">
    <label for="set"><input type="number" id="set" step="1" value="0">秒</label>
    <button id="btn">确定</button>
    <button id="reset">重置</button>    
</div>
<script>
var timer;
reset.onclick = function(){
    history.go();
}
btn.onclick = function(){
    if(timer) return;
    set.setAttribute('disabled','disabled');
    timer = setInterval(function(){
        if(Number(set.value) === 0){
            clearInterval(timer);
            timer = 0;
            set.removeAttribute('disabled');
            return;
        }
        set.value = Number(set.value) - 1;
    },1000);
}    
</script>

【2】正确倒计时

  由放大计时器的运营机制,大家知晓每间隔一千ms去改动时间的作法并不牢靠。更加纯粹地做法,应该是与系统的周转时刻作为参照,倒计时的光阴转移与系统的时光退换同步,到达标准倒计时的效果与利益 

  [注意]此部分中,供给经过取模运算和除法运算举办时、分、秒的乘除,详细景况运动至此

<div id="myDiv">
    <label for="hour"><input type="number" id="hour" min="0" max="23" step="1" value="0" />时</label>
    <label for="minute"><input type="number" id="minute" min="0" max="59" step="1" value="0" />分</label>
    <label for="second"><input type="number" id="second" min="0" max="23" step="1" value="0" />秒</label>
    <button id="btn">确定</button>
    <button id="reset">重置</button>
</div>
<script>
var timer;
//输入限制
hour.onchange = function(){
    if(Number(this.value) !== Number(this.value)) this.value = 0;
    if(this.value > 23) this.value = 23;
    if(this.value < 0) this.value = 0;
}
second.onchange = minute.onchange = function(){
    if(Number(this.value) !== Number(this.value)) this.value = 0;
    if(this.value > 59) this.value = 59;
    if(this.value < 0) this.value = 0;
}
reset.onclick = function(){
    history.go();
}
btn.onclick = function(){
    if(timer) return;
    for(var i = 0; i < 3; i++){
        myDiv.getElementsByTagName('input')[i].setAttribute('disabled','disabled');
    }
    //原始储存值
    var setOri = hour.value*3600 + minute.value*60 + second.value*1;
    //原始系统时间值
    var timeOri = (new Date()).getTime();
    //现在所剩时间值
    var setNow;
    cancelAnimationFrame(timer);
    timer = requestAnimationFrame(function fn(){
        //当前系统时间值
        var timeNow = (new Date()).getTime();
        //使系统时间的差值与设置时间的差值相等,来获得正常的时间变化
        setNow = setOri - Math.floor((timeNow - timeOri)/1000);
        hour.value = Math.floor((setNow%86400)/3600);
        minute.value = Math.floor((setNow%3600)/60);
        second.value = Math.floor(setNow%60);
        timer = requestAnimationFrame(fn);
        if(setNow==0){
            cancelAnimationFrame(timer);
            timer = 0;
            btn.innerHTML = '计时结束';
            for(var i = 0; i < 3; i++){
                myDiv.getElementsByTagName('input')[i].removeAttribute('disabled');
            }
            setTimeout(function(){
                btn.innerHTML = '确定';
            },1000)            
        }

    })
}
</script>

 

<div id="myDiv"></div>
<script>
myDiv.innerHTML = /\d\d:\d\d:\d\d/.exec(new Date().toString())[0];
setInterval(function(){
myDiv.innerHTML = /\d\d:\d\d:\d\d/.exec(new Date().toString())[0]; 
},500);
</script>

2.设置按键,分别是初阶,甘休,重新苏醒设置

2.装置开关,分别是从头,截至,重新初始化

秒表

【1】简易秒表

  秒表与倒计时的笔触同样,比较来说,更加简约。每间隔100ms扩展100ms就可以

<div id="myDiv">
    <label for="set"><input id="set" value="0"></label>
    <button id="btn">开始</button>
    <button id="reset">重置</button>    
</div>
<script>
var timer;
var con = 'off';
var num = 0;
reset.onclick = function(){
    history.go();
}
btn.onclick = function(){
    if(con === 'off'){
        set.setAttribute('disabled','disabled');
        con = 'on';
        btn.innerHTML = '暂停';
        timer = setInterval(function(){
            num+= 100;
            var minute = Math.floor(num/1000/60);
            var second = Math.floor(num/1000);
            var ms = Math.floor(num%1000)/100;
            set.value = minute + ' : ' + second + ' . ' + ms; 
        },100);
    }else{
        clearInterval(timer);
        con = 'off';
        btn.innerHTML = '开始';    
    }
}    
</script>

【2】准确秒表

  与倒计时类似,使用定时器的时刻间隔作为时间更动的参照是不可信的。更可相信的做法,应该是使用系统的时光改换作为秒表的成形的参阅

<div id="myDiv">
    <label for="set"><input id="set" value="0"></label>
    <button id="btn">开始</button>
    <button id="reset">重置</button>    
</div>
<script>
var timer;
var con = 'off';
//ori表示初始的系统时间
var ori;
//dis表示当前运行时的秒数(动态)
var dis = 0;
//last储存暂停时的秒数(静态)
var last = 0;
reset.onclick = function(){
    history.go();
}
btn.onclick = function(){
    if(con === 'off'){
        set.setAttribute('disabled','disabled');
        con = 'on';
        btn.innerHTML = '暂停';
        //保留已经走过的秒数的系统时间
        ori = (new Date()).getTime() - last; 
        timer = requestAnimationFrame(function fn(){

            dis = (new Date()).getTime() - ori;
            cancelAnimationFrame(timer);
            timer = requestAnimationFrame(fn);
            var minute = Math.floor(dis/1000/60);
            var second = Math.floor(dis/1000);
            var ms = Math.floor(dis%1000);
            set.value = minute + ' : ' + second + ' . ' + ms; 
        });
    }else{
        cancelAnimationFrame(timer);
        btn.innerHTML = '开始';    
        con = 'off';
        last = dis;
    }
}    
</script>

 

图片 4

3,通过字符串调换来数字,和数字转变到字符串来拓展秒表的统一计划

3,通过字符串调换到数字,和数字转变到字符串来进行秒表的规划

闹钟

  挂钟其实正是在石英钟的根底上增添二个约定期间设置,石英钟设置要求将安装时间调换来距离一九七〇年2月1日的皮秒数,然后再算出与近来岁月的差值。随着当前时间的不仅仅增添,当差值为0时,石英钟响起

<div id="myDiv"></div>
<div id="con">
    <label for="hour"><input type="number" id="hour" min="0" max="23" step="1" value="0" />时</label>
    <label for="minute"><input type="number" id="minute" min="0" max="59" step="1" value="0" />分</label>
    <label for="second"><input type="number" id="second" min="0" max="23" step="1" value="0" />秒</label>
    <button id="btn">确定</button>
    <button id="reset">重置</button>
</div>
<div id="show"></div>
<script>
var timer;
//所剩时间
var dis;
myDiv.innerHTML = /\d\d:\d\d:\d\d/.exec(new Date().toString())[0];
setInterval(function(){
    myDiv.innerHTML = /\d\d:\d\d:\d\d/.exec(new Date().toString())[0];    
},100);
reset.onclick = function(){
    history.go();
}
btn.onclick = function(){
    //原始储存值
    var setOri = hour.value*3600 + minute.value*60 + second.value*1;
    //原始值转换为1970年的毫秒数
    var setMs = +new Date(new Date().toDateString()) + setOri*1000;
    //如果设置的时间早于当前时间,则设置无效
    if(setMs < +new Date()){
        return;
    }
    for(var i = 0; i < 3; i++){
        con.getElementsByTagName('input')[i].setAttribute('disabled','disabled');
    }
    cancelAnimationFrame(timer);
    timer = requestAnimationFrame(function fn(){
        //算出设置时间与当前时间的差值
        dis = Math.ceil((setMs - (new Date()).getTime())/1000);
        var showHour = Math.floor((dis%86400)/3600);
        var showMinute = Math.floor((dis%3600)/60);
        var showSecond = Math.floor(dis%60);
        timer = requestAnimationFrame(fn);
        show.innerHTML = '距离预定时间还有 ' + showHour + '小时 ' + showMinute + '分 ' + showSecond + '秒';
        //当差值为0时,时间到
        if(dis==0){
            cancelAnimationFrame(timer);
            btn.innerHTML = '时间到了';
            for(var i = 0; i < 3; i++){
                con.getElementsByTagName('input')[i].removeAttribute('disabled');
            }
            timer = setTimeout(function(){
                btn.innerHTML = '确定';
            },1000)            
        }

    });
}
</script>

 

倒计时

 

 

最后

  作为沙漏来讲,最辛劳的地点是反应计时器管理。假诺,沙漏只开启不闭馆,则会导致机械漏刻叠合效果,使得运转越来越快。所以,先关闭再启用停车计时器是三个好习于旧贯

  上边多少个应用加上以前的日历效果,基本包罗了关于日期和岁月的运用。基本原理都类似,剩下的正是CSS美化和js代码优化难题

  款待沟通

【1】简易倒计时

  1 #import "ViewController.h"
  2 
  3 #import "UIView+FrameExtension.h"
  4 
  7 #define kDeviceWidth [UIScreen mainScreen].bounds.size.width
  8 
  9 #define kDeviceHeight [UIScreen mainScreen].bounds.size.height
 10 
 13 @interface ViewController (){
 14 
 15     UILabel *_lbl1;
 16 
 17     UILabel *_lbl2;
 18 
 19     UILabel *_lbl3;
 20 
 21     UILabel *_lbl4;

 25     NSTimer *_timer;
 26 
 27     BOOL    _isRunning;
 28 
 29 }
 33 @end
 34 
 37 @implementation ViewController
 38 
 41 - (void)viewDidLoad {
 42 
 43     [super viewDidLoad];
 44 
 47     [self createLabel];     //创建7个标签
 48 
 49     [self createTimer];     //创建1个定时器
 50 
 51     [self createButton];    //创建3个按
 52 
 53 }
 54 
 57 -(void)createLabel{

 61     UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(90, 200, 30, 40)];
 62 
 63     lbl1.text = @"00";
 64 
 65     lbl1.textColor = [UIColor redColor];
 66 
 67     [self.view addSubview:lbl1];
 68 
 69     _lbl1 = lbl1;
 70 
 73     UILabel *lbl11 = [[UILabel alloc]initWithFrame:CGRectMake(lbl1.right, lbl1.y, 10, lbl1.height)];
 74 
 75     lbl11.text = @":";
 76 
 77     lbl11.textColor = [UIColor redColor];
 78 
 79     [self.view addSubview:lbl11];
 80 
 83      UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(lbl11.right, lbl1.y, lbl1.width, lbl1.height)];
 84 
 85     lbl2.text = @"00";
 86 
 87     lbl2.textColor = [UIColor redColor];
 88 
 89     [self.view addSubview:lbl2];
 90 
 91     _lbl2 = lbl2;
 92 
 95     UILabel *lbl22 = [[UILabel alloc]initWithFrame:CGRectMake(lbl2.right, lbl1.y, lbl11.width, lbl1.height)];
 96 
 97     lbl22.text = @":";
 98 
 99     lbl22.textColor = [UIColor redColor];
100 
101     [self.view addSubview:lbl22];
102 
103     UILabel *lbl3 = [[UILabel alloc]initWithFrame:CGRectMake(lbl22.right, lbl1.y, lbl1.width, lbl1.height)];
104 
105     lbl3.text = @"00";
106 
107     lbl3.textColor = [UIColor redColor];
108 
109     [self.view addSubview:lbl3];
110 
111     _lbl3 = lbl3;
112 
115     UILabel *lbl33 = [[UILabel alloc]initWithFrame:CGRectMake(lbl3.right, lbl1.y, lbl11.width, lbl1.height)];
116 
117     lbl33.text = @".";
118 
119     lbl33.textColor = [UIColor redColor];
120 
121     [self.view addSubview:lbl33]; 
122 
123     UILabel *lbl4 = [[UILabel alloc]initWithFrame:CGRectMake(lbl33.right, lbl1.y, lbl1.width, lbl1.height)];
124 
125     lbl4.text = @"00";
126 
127     lbl4.textColor = [UIColor redColor];
128 
129     [self.view addSubview:lbl4];
130 
131     _lbl4 = lbl4;
132 
137 }

141 -(void)createButton{
142  
146 
147     UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(70, _lbl1.bottom+20, 60, 40)];
148 
149     [btn1 setTitle:@"开始" forState:UIControlStateNormal];
150 
151     [self.view addSubview:btn1];
152 
153     [btn1 setBackgroundImage:[UIImage imageNamed:@"logoff_btn_s"] forState:UIControlStateNormal];
154 
155     [btn1 setBackgroundImage:[UIImage imageNamed:@"submit_discussion_n"] forState:UIControlStateHighlighted];
156 
157     [btn1 addTarget:self action:@selector(start ) forControlEvents:UIControlEventTouchUpInside];
158 
163     UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(btn1.right+10, btn1.y, btn1.width, btn1.height)];
164 
165     [btn2 setTitle:@"停止" forState:UIControlStateNormal];
166 
167     [self.view addSubview:btn2];
168 
169     [btn2 setBackgroundImage:[UIImage imageNamed:@"logoff_btn_s"] forState:UIControlStateNormal];
170 
171     [btn2 setBackgroundImage:[UIImage imageNamed:@"submit_discussion_n"] forState:UIControlStateHighlighted];
172 
173     [btn2 addTarget:self action:@selector(stop ) forControlEvents:UIControlEventTouchUpInside];
174 
179     UIButton *btn3 = [[UIButton alloc]initWithFrame:CGRectMake(btn2.right+10,btn1.y , btn1.width, btn1.height)];
180 
181     [btn3 setTitle:@"复位" forState:UIControlStateNormal];
182 
183     [self.view addSubview:btn3];
184 
185     [btn3 setBackgroundImage:[UIImage imageNamed:@"logoff_btn_s"] forState:UIControlStateNormal];
186 
187     [btn3 setBackgroundImage:[UIImage imageNamed:@"submit_discussion_n"] forState:UIControlStateHighlighted];
188 
189     [btn3 addTarget:self action:@selector(fuwei ) forControlEvents:UIControlEventTouchUpInside];
190 
191 }
192 
195 -(void)start{

199     [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
200 
201     _isRunning = YES;
202   
204 
205     if (_isRunning) {       //表示定时器正在运行
206 
207         [_timer setFireDate:[NSDate distantPast]];
208 
211     }else{
212 
213         [_timer setFireDate:[NSDate distantFuture]];
214 
215     }
216 
217 }
218 
221 -(void)stop{
222 
225     _isRunning = !_isRunning;
226 
229     if (_isRunning) {       // 表示定时器正在运行
230 
233     }else{
234 
235         [_timer setFireDate:[NSDate distantFuture]];
236 
237     }
238 
239     _isRunning = NO;
240 
241 }
242 
245 -(void)fuwei{
246 
251         NSString* str4 = _lbl4.text;
252 
253         NSString* str3 = _lbl3.text;
254 
255         NSString* str2 = _lbl2.text;
256 
257         NSString* str1 = _lbl1.text;
258 
261         int x4 = [str4 intValue];
262 
263         int x3 = [str3 intValue];
264 
265         int x2 = [str2 intValue];
266 
267         int x1 = [str1 intValue];
268 
271         x4 = x3 = x2 = x1 = 0;
272 
275         NSString* str44 = [NSString stringWithFormat:@"%02d",x4];
276 
277         NSString* str33 = [NSString stringWithFormat:@"%02d",x3];
278 
279         NSString* str22 = [NSString stringWithFormat:@"%02d",x2];
280 
281         NSString* str11 = [NSString stringWithFormat:@"%02d",x1];
282 
283         _lbl4.text = str44;
284 
285         _lbl3.text = str33;
286 
287         _lbl2.text = str22;
288 
289         _lbl1.text = str11;
290 
293     _isRunning = !_isRunning;
294 
297     if (_isRunning) {       // 表示定时器正在运行
298 

301     }else{
302 
303         [_timer setFireDate:[NSDate distantFuture]];
304 
305     }
306 
307     _isRunning = NO;
308 

311 }
312 

315 -(void)createTimer{

319     _timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(move ) userInfo:nil repeats:YES];
320 
323     _isRunning = NO;
324 
327     //     下面这种方法创建的定时器,会自动的加入运行循环
328 
329     //    _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(move) userInfo:nil repeats:YES];
330 
331 }
332 
334 
335 -(void)move{
336 
339      NSString* str4 = _lbl4.text;
340 
341      NSString* str3 = _lbl3.text;
342 
343      NSString* str2 = _lbl2.text;
344 
345      NSString* str1 = _lbl1.text;
346 
349     int x4 = [str4 intValue];
350 
351     int x3 = [str3 intValue];
352 
353     int x2 = [str2 intValue];
354 
355     int x1 = [str1 intValue];
356 
359     x4++;
360 
363     if (x4 == 100 ) {
364 
365         x3 = x3 + 1;
366 
367         x4 = 0;
368 
369         if (x3 == 60) {
370 
371             x2 = x2 + 1;
372 
373             x3 = 0;
374 
375             if (x2 == 60 ) {
376 
377                 x1 = x1 + 1;
378 
379                 x2 = 0;
380 
381                 if (x1 == 24 ) {
382 
383                     x4 = x3 = x2 = x1 = 0;
384 
385                 }
386 
387             }
388 
389         }
390 
391     }
392    
394 
395     NSString* str44 = [NSString stringWithFormat:@"%02d",x4];
396 
397     NSString* str33 = [NSString stringWithFormat:@"%02d",x3];
398 
399     NSString* str22 = [NSString stringWithFormat:@"%02d",x2];
400 
401     NSString* str11 = [NSString stringWithFormat:@"%02d",x1];
402 
403     _lbl4.text = str44;
404 
405     _lbl3.text = str33;
406 
407     _lbl2.text = str22;
408 
409     _lbl1.text = str11;   
410 
411 }
412 
413  @end


开始
  1 #import "ViewController.h"
  2 
  3 #import "UIView+FrameExtension.h"
  4 
  7 #define kDeviceWidth [UIScreen mainScreen].bounds.size.width
  8 
  9 #define kDeviceHeight [UIScreen mainScreen].bounds.size.height
 10 
 13 @interface ViewController (){
 14 
 15     UILabel *_lbl1;
 16 
 17     UILabel *_lbl2;
 18 
 19     UILabel *_lbl3;
 20 
 21     UILabel *_lbl4;

 25     NSTimer *_timer;
 26 
 27     BOOL    _isRunning;
 28 
 29 }
 33 @end
 34 
 37 @implementation ViewController
 38 
 41 - (void)viewDidLoad {
 42 
 43     [super viewDidLoad];
 44 
 47     [self createLabel];     //创建7个标签
 48 
 49     [self createTimer];     //创建1个定时器
 50 
 51     [self createButton];    //创建3个按
 52 
 53 }
 54 
 57 -(void)createLabel{

 61     UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(90, 200, 30, 40)];
 62 
 63     lbl1.text = @"00";
 64 
 65     lbl1.textColor = [UIColor redColor];
 66 
 67     [self.view addSubview:lbl1];
 68 
 69     _lbl1 = lbl1;
 70 
 73     UILabel *lbl11 = [[UILabel alloc]initWithFrame:CGRectMake(lbl1.right, lbl1.y, 10, lbl1.height)];
 74 
 75     lbl11.text = @":";
 76 
 77     lbl11.textColor = [UIColor redColor];
 78 
 79     [self.view addSubview:lbl11];
 80 
 83      UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(lbl11.right, lbl1.y, lbl1.width, lbl1.height)];
 84 
 85     lbl2.text = @"00";
 86 
 87     lbl2.textColor = [UIColor redColor];
 88 
 89     [self.view addSubview:lbl2];
 90 
 91     _lbl2 = lbl2;
 92 
 95     UILabel *lbl22 = [[UILabel alloc]initWithFrame:CGRectMake(lbl2.right, lbl1.y, lbl11.width, lbl1.height)];
 96 
 97     lbl22.text = @":";
 98 
 99     lbl22.textColor = [UIColor redColor];
100 
101     [self.view addSubview:lbl22];
102 
103     UILabel *lbl3 = [[UILabel alloc]initWithFrame:CGRectMake(lbl22.right, lbl1.y, lbl1.width, lbl1.height)];
104 
105     lbl3.text = @"00";
106 
107     lbl3.textColor = [UIColor redColor];
108 
109     [self.view addSubview:lbl3];
110 
111     _lbl3 = lbl3;
112 
115     UILabel *lbl33 = [[UILabel alloc]initWithFrame:CGRectMake(lbl3.right, lbl1.y, lbl11.width, lbl1.height)];
116 
117     lbl33.text = @".";
118 
119     lbl33.textColor = [UIColor redColor];
120 
121     [self.view addSubview:lbl33]; 
122 
123     UILabel *lbl4 = [[UILabel alloc]initWithFrame:CGRectMake(lbl33.right, lbl1.y, lbl1.width, lbl1.height)];
124 
125     lbl4.text = @"00";
126 
127     lbl4.textColor = [UIColor redColor];
128 
129     [self.view addSubview:lbl4];
130 
131     _lbl4 = lbl4;
132 
137 }

141 -(void)createButton{
142  
146 
147     UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(70, _lbl1.bottom+20, 60, 40)];
148 
149     [btn1 setTitle:@"开始" forState:UIControlStateNormal];
150 
151     [self.view addSubview:btn1];
152 
153     [btn1 setBackgroundImage:[UIImage imageNamed:@"logoff_btn_s"] forState:UIControlStateNormal];
154 
155     [btn1 setBackgroundImage:[UIImage imageNamed:@"submit_discussion_n"] forState:UIControlStateHighlighted];
156 
157     [btn1 addTarget:self action:@selector(start ) forControlEvents:UIControlEventTouchUpInside];
158 
163     UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(btn1.right+10, btn1.y, btn1.width, btn1.height)];
164 
165     [btn2 setTitle:@"停止" forState:UIControlStateNormal];
166 
167     [self.view addSubview:btn2];
168 
169     [btn2 setBackgroundImage:[UIImage imageNamed:@"logoff_btn_s"] forState:UIControlStateNormal];
170 
171     [btn2 setBackgroundImage:[UIImage imageNamed:@"submit_discussion_n"] forState:UIControlStateHighlighted];
172 
173     [btn2 addTarget:self action:@selector(stop ) forControlEvents:UIControlEventTouchUpInside];
174 
179     UIButton *btn3 = [[UIButton alloc]initWithFrame:CGRectMake(btn2.right+10,btn1.y , btn1.width, btn1.height)];
180 
181     [btn3 setTitle:@"复位" forState:UIControlStateNormal];
182 
183     [self.view addSubview:btn3];
184 
185     [btn3 setBackgroundImage:[UIImage imageNamed:@"logoff_btn_s"] forState:UIControlStateNormal];
186 
187     [btn3 setBackgroundImage:[UIImage imageNamed:@"submit_discussion_n"] forState:UIControlStateHighlighted];
188 
189     [btn3 addTarget:self action:@selector(fuwei ) forControlEvents:UIControlEventTouchUpInside];
190 
191 }
192 
195 -(void)start{

199     [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
200 
201     _isRunning = YES;
202   
204 
205     if (_isRunning) {       //表示定时器正在运行
206 
207         [_timer setFireDate:[NSDate distantPast]];
208 
211     }else{
212 
213         [_timer setFireDate:[NSDate distantFuture]];
214 
215     }
216 
217 }
218 
221 -(void)stop{
222 
225     _isRunning = !_isRunning;
226 
229     if (_isRunning) {       // 表示定时器正在运行
230 
233     }else{
234 
235         [_timer setFireDate:[NSDate distantFuture]];
236 
237     }
238 
239     _isRunning = NO;
240 
241 }
242 
245 -(void)fuwei{
246 
251         NSString* str4 = _lbl4.text;
252 
253         NSString* str3 = _lbl3.text;
254 
255         NSString* str2 = _lbl2.text;
256 
257         NSString* str1 = _lbl1.text;
258 
261         int x4 = [str4 intValue];
262 
263         int x3 = [str3 intValue];
264 
265         int x2 = [str2 intValue];
266 
267         int x1 = [str1 intValue];
268 
271         x4 = x3 = x2 = x1 = 0;
272 
275         NSString* str44 = [NSString stringWithFormat:@"%02d",x4];
276 
277         NSString* str33 = [NSString stringWithFormat:@"%02d",x3];
278 
279         NSString* str22 = [NSString stringWithFormat:@"%02d",x2];
280 
281         NSString* str11 = [NSString stringWithFormat:@"%02d",x1];
282 
283         _lbl4.text = str44;
284 
285         _lbl3.text = str33;
286 
287         _lbl2.text = str22;
288 
289         _lbl1.text = str11;
290 
293     _isRunning = !_isRunning;
294 
297     if (_isRunning) {       // 表示定时器正在运行
298 

301     }else{
302 
303         [_timer setFireDate:[NSDate distantFuture]];
304 
305     }
306 
307     _isRunning = NO;
308 

311 }
312 

315 -(void)createTimer{

319     _timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(move ) userInfo:nil repeats:YES];
320 
323     _isRunning = NO;
324 
327     //     下面这种方法创建的定时器,会自动的加入运行循环
328 
329     //    _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(move) userInfo:nil repeats:YES];
330 
331 }
332 
334 
335 -(void)move{
336 
339      NSString* str4 = _lbl4.text;
340 
341      NSString* str3 = _lbl3.text;
342 
343      NSString* str2 = _lbl2.text;
344 
345      NSString* str1 = _lbl1.text;
346 
349     int x4 = [str4 intValue];
350 
351     int x3 = [str3 intValue];
352 
353     int x2 = [str2 intValue];
354 
355     int x1 = [str1 intValue];
356 
359     x4++;
360 
363     if (x4 == 100 ) {
364 
365         x3 = x3 + 1;
366 
367         x4 = 0;
368 
369         if (x3 == 60) {
370 
371             x2 = x2 + 1;
372 
373             x3 = 0;
374 
375             if (x2 == 60 ) {
376 
377                 x1 = x1 + 1;
378 
379                 x2 = 0;
380 
381                 if (x1 == 24 ) {
382 
383                     x4 = x3 = x2 = x1 = 0;
384 
385                 }
386 
387             }
388 
389         }
390 
391     }
392    
394 
395     NSString* str44 = [NSString stringWithFormat:@"%02d",x4];
396 
397     NSString* str33 = [NSString stringWithFormat:@"%02d",x3];
398 
399     NSString* str22 = [NSString stringWithFormat:@"%02d",x2];
400 
401     NSString* str11 = [NSString stringWithFormat:@"%02d",x1];
402 
403     _lbl4.text = str44;
404 
405     _lbl3.text = str33;
406 
407     _lbl2.text = str22;
408 
409     _lbl1.text = str11;   
410 
411 }
412 
413  @end


开始

  简易倒计时正是每1s因此setInterval将安装的日子减去1来实现要求