【转】iOS中定时器NSTimer的使用
原文网址:http://www.cnblogs.com/zhulin/archive/2012/02/02/2335866.html
1、初始化
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
注:不用scheduled方式初始化的,需要手动addTimer:forMode: 将timer添加到一个runloop中。
而scheduled的初始化方法将以默认mode直接添加到当前的runloop中.
举例:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
或
NSTimer *myTimer = [NSTimertimerWithTimeInterval:3.0 target:selfselector:@selector(timerFired:)userInfo:nilrepeats:NO];
[[NSRunLoopcurrentRunLoop] addTimer:myTimerforMode:NSDefaultRunLoopMode];
2、触发(启动)
当定时器创建完(不用scheduled的,添加到runloop中后,该定时器将在初始化时指定的timeInterval秒后自动触发。
可以使用-(void)fire;方法来立即触发该定时器;
注:You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.
在重复执行的定时器中调用此方法后立即触发该定时器,但不会中断其之前的执行计划;
在不重复执行的定时器中调用此方法,立即触发后,就会使这个定时器失效。
3、停止
- (void)invalidate;
这个是唯一一个可以将计时器从runloop中移出的方法。
注:
NSTimer可以精确到50-100毫秒.
NSTimer不是绝对准确的,而且中间耗时或阻塞错过下一个点,那么下一个点就pass过去了.
【转】iOS中定时器NSTimer的使用的更多相关文章
- IOS中定时器NSTimer的开启与关闭
调用一次计时器方法: myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scro ...
- iOS中定时器NSTimer的使用-备用
1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...
- 【转】IOS中定时器NSTimer的开启与关闭
原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...
- 【转】 IOS中定时器NSTimer的开启与关闭
原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...
- 蜗牛爱课- iOS中定时器NSTimer使用
调用一次计时器方法: //不重复,只调用一次.timer运行一次就会自动停止运行 self.locationTimer = [NSTimer target:self selector: @selec ...
- iOS中定时器NSTimer的使用/开启与关闭
一.只调用一次计时器方法: //不重复,只调用一次.timer运行一次就会自动停止运行 myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 ...
- ios 中定时器:NSTimer, CADisplayLink, GCD
#import "ViewController.h" #import "RunloopViewController.h" @interface ViewCont ...
- iOS 中的 NSTimer
iOS 中的 NSTimer NSTimer fire 我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: @interface Detail ...
- iOS中的NSTimer 和 Android 中的Timer
首先看iOS的, Scheduling Timers in Run Loops A timer object can be registered in only one run loop at a t ...
随机推荐
- [设计模式] 9 装饰者模式 Decorator
转:http://www.jellythink.com/archives/171#prettyPhoto 什么是装饰模式? 在GOF的<设计模式:可复用面向对象软件的基础>一书中对装饰模式 ...
- Caffe训练好的网络对图像分类
对于训练好的Caffe 网络 输入:彩色or灰度图片 做minist 下手写识别分类,不能直接使用,需去除均值图像,同时将输入图像像素归一化到0-1直接即可. #include <caffe/c ...
- 宏基5750G 不能用内置无线网卡上网
宏基5750G 不能用内置无线网卡上网 具体体现在: 1.--> 搜索不到无线网络: 2.点击“打开网络和共享中心”-->没有“管理无线网络”一项.如果正常的话应该是这样的: 3.“我的电 ...
- DelayedOperationPurgatory之DelayedOperation pool
purgatory就是炼狱的意思. 当一个DelayedOperation需要被delay时,它就被放到DelayedOperationPurgatory,相当于进行一个等待池.上一篇blog提到过, ...
- Unity3D开发(五):Unity3D 4.x 使用Mecanim实现连击(转)
原地址:http://www.unitymanual.com/blog-1801-1221.html unity3d 4.x 版本之后提供了一种新的动画机制Mecanim,虽然目前还支持之前的Anim ...
- Lua print on the same line
In Pascal, I have write and writeln. Apparently Lua's print is similar to writeln of Pascal. Do we h ...
- Core Java Interview Question Answer
This is a new series of sharing core Java interview question and answer on Finance domain and mostly ...
- StringTokenizer用法
import java.util.StringTokenizer; public class StringTokenizerTest { public static void main(String[ ...
- POJ 1663
#include<iostream>//cheng da cai zi using namespace std; int main() { int time; cin>>tim ...
- TopCoder 649 div1 & div2
最近一场TC,做得是在是烂,不过最后challenge阶段用一个随机数据cha了一个明显错误的代码,最后免于暴跌rating,还涨了一点.TC题目质量还是很高的,非常锻炼思维,拓展做题的视野,老老实实 ...