iOS中定时器NSTimer的使用/开启与关闭

一.只调用一次计时器方法:

  //不重复,只调用一次。timer运行一次就会自动停止运行

  myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(function:) userInfo:nil repeats:NO];

二.重复调用计时器方法:

  //每2秒运行一次function方法。

  timer =  [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(function:) userInfo:nil repeats:YES];

  注意 : 将计数器的repeats设置为YES的时候,self的引用计数会加1。因此可能会导致self(即viewController)不能释放,所以必须在viewDidDisappear方法里,将计数器timer停止,否则可能会导致内存泄露。

三.彻底停止timer:(这是timer永久的停止, 停止后, 一定要将timer赋空, 否则还是没有释放, 会造成不必要的内存开销)

  //取消定时器

  [timer invalidate];

  timer = nil;

四.在页面消失的时候关闭定时器,然后等页面再次打开的时候,又开启定时器

  //页面将要进入前台,开启定时器

  -(void)viewWillAppear:(BOOL)animated

  {

      //开启定时器

      [self.myTimer setFireDate:[NSDate distantPast]]; //很远的过去

  }

  //页面消失,进入后台不显示该页面,关闭定时器

  -(void)viewDidDisappear:(BOOL)animated

  {     

      //关闭定时器

        [self.myTimer setFireDate:[NSDate distantFuture]];  //很远的将来

  }

时间: 2024-08-28 06:28:53

iOS中定时器NSTimer的使用/开启与关闭的相关文章

iOS中定时器NStimer的使用

1,NStimer 的初始化方式有下面四种,分为timerWithTimeInterval和scheduledTimerWithTimeInterval开头的 1 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo; 2 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTime

【转】 IOS中定时器NSTimer的开启与关闭

原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: [cpp] view plain copy myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scrollTimer) userInfo:nil repeats:NO]; //不重复,只调用一次.timer运行一次就会自动停止运行 重复调用计时

IOS中定时器NSTimer的开启与关闭

本文转载至 myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scrollTimer) userInfo:nil repeats:NO]; //不重复,只调用一次.timer运行一次就会自动停止运行 重复调用计时器方法: [cpp] view plaincopy timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 targe

【转】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 *)scheduledTimerWithTime

iOS中定时器NSTimer使用

调用一次计时器方法: //不重复,只调用一次.timer运行一次就会自动停止运行 self.locationTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector: @selector(LocationTimer) userInfo:nil repeats:NO]; 重复调用计时器方法: //每1秒运行一次LocationTimer方法 self.locationTimer = [NSTimer schedul

IOS中定时器NSTimer

调用一次计时器方法: [cpp]  view plain copy   myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scrollTimer) userInfo:nil repeats:NO]; //不重复,只调用一次.timer运行一次就会自动停止运行 重复调用计时器方法: [cpp]  view plain copy   timer =  [NSTimer schedu

iOS 中的 NSTimer

iOS 中的 NSTimer NSTimer fire 我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: @interface DetailViewController () @property (nonatomic, weak) NSTimer *timer; @end @implementation DetailViewController - (IBAction)fireButtonPressed:(id)sender { _ti

iOS中的NSTimer 和 Android 中的Timer

首先看iOS的, Scheduling Timers in Run Loops A timer object can be registered in only one run loop at a time, although it can be added to multiple run loop modes within that run loop. There are three ways to create a timer: Use the scheduledTimerWithTimeI

iOS中定时器

转载自  http://www.cocoachina.com/ios/20150519/11857.html 在软件开发过程中,我们常常需要在某个时间后执行某个方法,或者是按照某个周期一直执行某个方法.在这个时候,我们就需要用到定时器. 然而,在iOS中有很多方法完成以上的任务,到底有多少种方法呢?经过查阅资料,大概有三种方法:NSTimer.CADisplayLink.GCD.接下来我就一一介绍它们的用法. 一.NSTimer 1. 创建方法 NSTimer *timer = [NSTimer