定时器 每一分钟调用一次 NSTimer-定时器的使用浅析

此帖转载别人的 贞娃儿 他的博客http://blog.sina.com.cn/s/blog_7b9d64af0101caaz.html
定时器
NSTimer 是做什么的? 我只是当做笔记,请勿喷,自己使用了,很好使用,有疑问可以交流

你想重复的执行一个特定任务,这个任务具有一定的延时。例如:只要你的程序在运行,你想每秒钟更新一次屏幕中的视图。 


1.在*.h 文件中定义一个定时器

@property (nonatomic,strong) NSTimer *paintingTimer;


2.在*.m中调用吧

//定时器执行的方法

-(void)paint:(NSTimer *)paramTimer{


   NSLog(@"定时器执行的方法");

}


//开始定时器

-(void) startPainting{

    

   // 定义一个NSTimer

   self.paintingTimer = [NSTimerscheduledTimerWithTimeInterval:1.0

                                                target:self

                                              selector:@selector(paint:) userInfo:nil

                                               repeats:YES];

}


//停止定时器

-(void) stopPainting{

    if (self.paintingTimer != nil){

       // 定时器调用invalidate后,就会自动执行release方法。不需要在显示的调用release方法

       [self.paintingTimer invalidate];

    }

}


-(void)viewDidAppear:(BOOL)animated{

    

   [selfstartPainting];// 开始定时器

  

}


-(void)viewDidDisappear:(BOOL)animated{

    

 [selfstopPainting];// 停止定时器


}


NSTimer的初始化方法<一>

scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:方法


+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)secondstarget:(id)targetselector:(SEL)aSelectoruserInfo:(id)userInforepeats:(BOOL)repeats


seconds:需要调用的毫秒数

target:调用方法需要发送的对象。即:发给谁

userInfo:发送的参数

repeats:指定定时器是否重复调用目标方法


在看一看输出:

 Painting

 Main Thread = {name = (null), num = 1}

 Current Thread = {name = (null), num =1}

 Painting

 Main Thread = {name = (null), num = 1}

 Current Thread = {name = (null), num =1}


发现一个问题,呵呵
全部都是主线程在执行,那样的话,就有点问题了,我们应该让它在其他线程来执行!你懂的!我想用Block或者Operation都能够解决这个事情吧。那就过吧~~~扯得有点跑题了!

现在做个比喻:

我们可以把调度一个计时器与启动汽车的引擎相比较。别调度的计时器就是运行中的引擎。没有被调度的计时器就是一个已经准备好启动但是还没有运行的引擎。我们在程序里面,无论何时,都可以调度和取消调度计时器,就像根据我们所处的环境,决定汽车的引擎室启动还是停止。如果你想要在程序中,手动的在某一个确定时间点调度计时器,可以使用NSTimer的类方法 timerWithTimeInterval:target:selector:userInfo:repeats:方法。

飘红部分!说的好经典!
以上的程序,确实,我们能够调用和停止定时器,但是,我们还不够“ 随心所欲”。因为 NSTimer在初始化后就马上开始执行了!!!

NSTimer的初始化方法<二>

如果,我们想在任何时候都能够随心所欲的 启动/停止 定时器。

咋办?不用急,还有 NSTimer的另一种初始化方法,能够满足我们的要求:

贴吧!

// 使用 timerWithTimeInterval 方法来实例化一个 NSTimer, 这时候 NSTimer 是不会启动的

self.paintingTimer = [NSTimer timerWithTimeInterval:1.0

                                        target:self

                                       selector:@selector(paint:)

                                       userInfo:nil

                                        repeats:YES];



//当需要调用时,可以把计时器添加到事件处理循环中

 [[NSRunLoop currentRunLoop]addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode];


忽然想问一句:NSRunLoop是什么?
循环运行!就像是程序的中枢神经,一直在运转着,并且监视着程序各种的事件、线程等等。
一旦出现了相关的事件,那么就开始调度,分配适当的对象来执行适当的操作!

NSTimer的初始化方法<三>

我们也可以用 timerWithTimeInterval方式来创建一个 NSTimer

贴:

-(void) startPainting{

   // 定义将调用的方法

    SEL selectorToCall = @selector(paint:);

   // SEL进行方法签名

    NSMethodSignature*methodSignature=[[self class]instanceMethodSignatureForSelector:selectorToCall];

   // 初始化NSInvocation

    NSInvocation *invocation =[NSInvocationinvocationWithMethodSignature:methodSignature];

    [invocation setTarget:self];

   [invocation setSelector:selectorToCall];

    self.paintingTimer = [NSTimer timerWithTimeInterval:1.0

                                     invocation:invocation

                                        repeats:YES];

    

// 当需要调用时,可以把计时器添加到事件处理循环中

    [[NSRunLoop currentRunLoop] addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode];

}


忽然想问一句: NSInvocation 是什么?

是Object-C中的消息传递着。它可以以“方法签名”的方式来封装一个对象的方法,并且在各个对象中传送!主要可以用在NSTimer中。



发布了30 篇原创文章 · 获赞 9 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/u010713935/article/details/53130847