GCD11: 创建计时器

你想重复的执行一个特定任务,这个任务具有一定的延时。

1.例如:只要你的程序在运 行,你想每秒钟更新一次屏幕中的视图:

- (void)paint:(NSTimer *)paramTimer{
    NSLog(@"Painting");
}
- (void)startPainting{
    self.paintingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(paint:) userInfo:nil repeats:YES];
}
- (void)stopPainting{
    if (self.paintingTimer != nil) {
        [self.paintingTimer invalidate];//invalidate[?n‘væl?de?t]使无效无价值
    }
}
- (void)applicationWillResignActive:(UIApplication *)application {
    [self stopPainting];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    [self startPainting];
}

  一旦你调用了这样的函数:scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:计时器会变成一个被调度的计时器,就会根据你的请求触发事件。

  一个被调度的计时器也就是这个计时器被添加到了一个事件处理循环中。

  稍后将通过一个例子来演示:创建一个没有被调度的计时器,然后手动在程序的主事件处理循环中调度它。

  这里有多种方法来创建、初始化和调度计时器。最简单的方法就是使用 NSTimer 的类方法 scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:,下面是该方法的参数介绍:

interval等待秒数  target接收事件的目标对象  selector目标对象里响应事件的方法 userInfo可传递信息,包含在传递的计时器中 repeat是否重复

  可以使用 NSTimer 的实例方法 invalidate 来停止和释放计时器。这不仅仅会释放计时器, 也会释放计时器拥有的对象(例如userInfo的对象)

2.

你也可以使用其他的方法来创建被调度的计时器。NSTimer 的类方法 scheduledTimerWithTimeInterval:invocation:repeats:就是其中之一:

- (void)startPainting{
    //2.
    /* Here is the selector that we want to call */
    SEL selectorToCall = @selector(paint:);
    //转成方法签名?Signature签名  instance实例
    NSMethodSignature *methodSignature = [[self class]instanceMethodSignatureForSelector:selectorToCall];
    //??
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
    [invocation setTarget:self];
    [invocation setSelector:selectorToCall];
    /* start a scheduled timer now*/
    self.paintingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 invocation:invocation repeats:YES];

}

3.如果你想要在程序中,手动的在某一个确定时间点调度计时器,可以使用 NSTimer 的类方法 timerWithTimeInterval:target:selector:userInfo:repeats:,当你准备好的时 候,可以把计时器添加到事件处理循环中:

- (void) startPainting{
   self.paintingTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(paint:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode];
}
时间: 2024-08-02 18:12:10

GCD11: 创建计时器的相关文章

sharepoint admin svc must be running in order to create deployment timer job 若要创建计时器作业,必须运行SVC

sharepoint admin svc must be running in order to create deployment timer job 若要创建计时器作业,必须运行SVC 最近使用PowerShell部署解决方案时,遇到问题: 解决办法是进入控制面板----管理工具----服务,找到SharePoint 2010 Administration,启动它. 再次执行命令,没有提示错误. 现在解决方案已经部署好了,你可以使用它了. 小贴士:请不要更改SharePoint 2010 A

iOS:三种常见计时器(NSTimer、CADisplayLink、dispatch_source_t)的使用

一.介绍 在iOS中,计时器是比较常用的,用于统计累加数据或者倒计时等,例如手机号获取验证码.计时器大概有那么三种,分别是:NSTimer.CADisplayLink.dispatch_source_t 二.使用 @property (strong,nonatomic)NSTimer *timer; @property (strong,nonatomic)CADisplayLink *displaylinkTimer; @property (strong,nonatomic)dispatch_s

C语言Windows程序设计—— 使用计时器

传统意义上的计时器是指利用特定的原理来测量时间的装置, 在古代, 常用沙漏.点燃一炷香等方式进行粗略的计时, 在现代科技的带动下, 计时水平越来越高, 也越来越精确, 之所以需要进行计时是在很多情况下我们需要知道时间已经过去了多少, 举例说, 上课下课的打铃. 考试时的计时.车站按时间间隔进行发车等. 不仅在日常生活中会应用到计时, 在一些电子设备中计时的普遍存在, 如手机里的闹钟.电子秒表.电子设备的定时关机等, 这些计时的目的都是相同的, 当达到一定时间后执行某件事, 计时器相当于提醒作用,

创建简单动画(一) --- 常规hud

先说下当前我为处理动画的思路: (新手上路, 老司机轻喷,如果有更好的实现方法请大神指教 感恩戴德) #1. 分析动画构成 #2. 如果是位移动画则考虑使用BasicAnimation或者KeyframeAnimation实现, 需要的话再搭配缓动函数 #3. 比较复杂的动画则考虑是否用UIBezierpath一帧帧来画 今天我们模仿做一个场景切换加载等待动画, 比如这样的 我们分析下这张图的构成 #1. 一个灰色的背景 #2. 一个白色的圆环 #3. 一个闭合的圆弧(白色部分) 看起来不是简单

aspx利用cookie值来停止silverlight中的计时器

一.silverlight与silverlight中可以利用委托(delegate)来刷新frame.Refresh() 1.在子类中定义委托捕捉关闭事件按钮 1 public delegate void onCloseClick(object sender, RoutedEventArgs e); 2 public onCloseClick onclose; 3 private void CancelButton_Click(object sender, RoutedEventArgs e)

用Quick3.3开发微信打飞机 (二) -------------------- 子弹和敌人的配置和创建

用Quick3.3简单开发微信打飞机02 [plain] view plaincopyprint? 这次将实现子弹的配置文件,敌人的配置文件,子弹的精灵和敌人的精灵.以及子弹的发射和敌人的产生. 子弹的分析: 游戏中不可能只有一种子弹,这样子弹就需要一个配置文件,配置文件中有子弹的类型,子弹的贴图,子弹的生命以及子弹的移动速度. 敌人的分析: 同样,游戏中会出现多种敌人,所以,也需要一个敌人的配置文件,其中包括敌人的类型,贴图,速度,生命等属性. 在这里新建了一个目录(data)来存放配置文件.

C# 计时器和计数

定义:System.Threading.Timer timer;int count;TextBox textBox1; 创建计时器和每秒要执行的方法:timer = new System.Threading.Timer(st =>{ ++count; textBox1.AppendText("计数:" + count.ToString() + "\n"); if (count == 100) timer.Change(Timeout.Infinite, Tim

第九篇 -- 定时器和计时器

效果: ui_timer.py # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'ui_timer.ui' # # Created by: PyQt5 UI code generator 5.13.0 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidg

【学习ios之路:UI系列】UIProgressView

实现类似读取数据进度条效果 代码如下: ①创建UIProgressView对象 1)定义属性 { NSTimer *_proTimer;//计时 } @property (nonatomic, retain) UIProgressView *proView; //显示进度信息 @property (nonatomic, retain) UILabel *proLabel; @property (nonatomic, assign) float proValue;//保存进度值 //创建控件UIP