码迷,mamicode.com
首页 > 其他好文 > 详细

NSTimer与运行循环

时间:2015-04-05 06:23:07      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

NSTimer准确吗?如果不准确,怎么办?

通常用来有一点时间跨度的周期性事件的处理! CDADisplayLink

 

 1 //
 2 //  HMViewController.m
 3 //  08-倒计时
 4 //
 5 //  Created by apple on 14-8-18.
 6 //  Copyright (c) 2014年 itcast. All rights reserved.
 7 //
 8 
 9 #import "HMViewController.h"
10 
11 @interface HMViewController () <UIAlertViewDelegate>
12 
13 @property (weak, nonatomic) IBOutlet UILabel *counterLabel;
14 
15 @property (nonatomic, strong) NSTimer *timer;
16 @end
17 
18 @implementation HMViewController
19 
20 /** 开始 */
21 - (IBAction)start
22 {
23     // 倒计时10秒,每秒更新一下Label的显示
24     // 计时器
25     /** 
26      参数说明 
27      1. 时间间隔,double
28      2. 监听时钟触发的对象
29      3. 调用方法
30      4. userInfo,可以是任意对象,通常传递nil
31      5. repeats:是否重复
32      */
33     self.counterLabel.text = @"10";
34     
35     // scheduledTimerWithTimeInterval 方法本质上就是创建一个时钟,
36     // 添加到运行循环的模式是DefaultRunLoopMode
37     // ----------------------------------------------
38     // 1>
39 //    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:@"hello timer" repeats:YES];
40     
41     // ----------------------------------------------
42     // 2> 与1等价
43 //    self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
44 //    // 将timer添加到运行循环
45 //    // 模式:默认的运行循环模式
46 //    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
47     
48     // ----------------------------------------------
49     // 3>
50     self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
51     // 将timer添加到运行循环
52     // 模式:NSRunLoopCommonModes的运行循环模式(监听滚动模式)
53     [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
54     //[self updateTimer:self.timer];
55 }
56 
57 /** 时钟更新方法 */
58 - (void)updateTimer:(NSTimer *)timer
59 {
60    // NSLog(@"%s", __func__);
61     // 1. 取出标签中的数字
62     int counter = self.counterLabel.text.intValue;
63     
64     // 2. 判断是否为零,如果为0,停止时钟
65     if (--counter < 0) {
66         // 停止时钟
67         [self pause];
68         
69         // 提示用户,提示框
70 //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"开始" message:@"开始啦......" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", @"哈哈", nil];
71 //
72 //        [alert show];
73         [[[UIAlertView alloc] initWithTitle:@"开始" message:@"开始啦......" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", @"哈哈", nil] show];
74     } else {
75         // CTRL + I
76         // 3. 修改数字并更新UI
77         self.counterLabel.text = [NSString stringWithFormat:@"%d", counter];
78     }
79 }
80 
81 /** 暂停 */
82 - (IBAction)pause
83 {
84     // 停止时钟,invalidate是唯一停止时钟的方法
85     // 一旦调用了invalidate方法,timer就无效了,如果再次启动时钟,需要重新实例化
86     [self.timer invalidate];
87 }
88 
89 #pragma mark - alertView代理方法
90 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
91 {
92     NSLog(@"%ldaaa-------", (long)buttonIndex);
93 }
94 
95 @end

 

NSTimer与运行循环

标签:

原文地址:http://www.cnblogs.com/liqiantu/p/4393369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!