蜗牛爱课- iOS中定时器NSTimer使用
调用一次计时器方法:
//不重复,只调用一次。timer运行一次就会自动停止运行
self.locationTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self
selector: @selector(LocationTimer) userInfo:nil repeats:NO];
重复调用计时器方法:
//每1秒运行一次LocationTimer方法
self.locationTimer = [NSTimer scheduledTimerWithTimeInterval: target:self
selector: @selector(LocationTimer) userInfo:nil repeats:YES];
注意:将计数器的repeats设置为YES的时候,self的引用计数会加1。因此可能会导致self(即viewController)不能release,所以,必须在viewWillAppear的时候,将计数器timer停止,否则可能会导致内存泄露。
停止timer的运行,但这个是永久的停止:
//取消定时器
[self.locationTimer invalidate];
要想实现:先停止,然后再某种情况下再次开启运行timer,可以使用下面的方法:
首先关闭定时器不能使用上面的方法,应该使用下面的方法:
//关闭定时器
[self.locationTimer setFireDate:[NSDate distantFuture]];
然后就可以使用下面的方法再此开启这个timer了:
//开启定时器
[self.locationTimer setFireDate:[NSDate distantPast]];
例子:比如,在页面消失的时候关闭定时器,然后等页面再次打开的时候,又开启定时器。
(主要是为了防止它在后台运行,暂用CPU)可以使用下面的代码实现:
//页面将要进入前台,开启定时器
-(void)viewWillAppear:(BOOL)animated
{
//开启定时器
[self.locationTimer setFireDate:[NSDate distantPast]];
}
//页面消失,进入后台不显示该页面,关闭定时器
-(void)viewDidDisappear:(BOOL)animated
{
//关闭定时器
[self.locationTimerr setFireDate:[NSDate distantFuture]];
}
OK,搞定。
版权声明:欢迎转载,请注明出处:蜗牛爱课 • 移动互联网
蜗牛爱课- iOS中定时器NSTimer使用的更多相关文章
- 蜗牛爱课 -- iOS 设计模式之模板模式
1 前言 模板方法模式是面向对象软件设计中一种非常简单的设计模式.其基本思想是在抽象类的一个方法定义“标准”算法.在这个方法中调用的基本操作由子类重载予以实现.这个方法成为“模板”.因为方法定义的算法 ...
- IOS中定时器NSTimer的开启与关闭
调用一次计时器方法: myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scro ...
- 【转】iOS中定时器NSTimer的使用
原文网址:http://www.cnblogs.com/zhulin/archive/2012/02/02/2335866.html 1.初始化 + (NSTimer *)timerWithTimeI ...
- 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运行一次就会自动停止运行 myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 ...
- ios 中定时器:NSTimer, CADisplayLink, GCD
#import "ViewController.h" #import "RunloopViewController.h" @interface ViewCont ...
- 蜗牛爱课 -- iOS 设置UIButton的字体的大小、显示位置、大小
/设置按钮上的自体的大小 //[btn setFont: [UIFont systemFontSize: 14.0]]; //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法 ...
随机推荐
- java学习之部分笔记2
1.变量 实例变量和局部变量 实例变量系统会自动初始化为0和null(string),局部变量必须设定初始值. 静态方法里只能引用静态变量 数据类型的自动转换! int—>long 2.构造方法 ...
- C#实现WinForm传值实例解析
C#实现WinForm传值的问题经常会做为公司面试的题目,那么作为学习C#以及WinForm传值,我们需要掌握哪些方法和思路呢?下面我们就向你介绍详细的思路和实现的具体步骤,希望对你有所帮助. C#实 ...
- MVC 授权过滤器 AuthorizeAttribute
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- GIS-开发例程、源代码、MapXtreme、Map (转)
[原创]MapXtreme实用技巧与源码10例 普通图片生成MapInfo格式电子地图的步骤 http://blog.csdn.net/hmbb2008/category/184134.aspx 基 ...
- hibernate:XXX is not mapped
hibernate:XXX is not mapped 检查项目中是否将hbm.xml引入
- HtmlParser基础教程
1.相关资料 官方文档:http://htmlparser.sourceforge.net/samples.html API:http://htmlparser.sourceforge.net/jav ...
- Android Studio rename module Can't rename root module
Android Studio修改工程根目录的时候会报错, rename module Can't rename root module. 主要是该工程已经打开,再命名必须要关闭改工程,就跟正在写一个t ...
- python-整理-面向对象
python的类和perl的类有相似之处,类的方法的第一个参数是表示类的对象自己,相当于c#的this python中定义类 class person: ''示例类,人'' count=0; def ...
- 手机触摸屏的JS事件
处理Touch事件能让你跟踪用户的每一根手指的位置.你可以绑定以下四种Touch事件: touchstart: // 手指放到屏幕上的时候触发 touchmove: // 手指在屏幕上移动的时候触发 ...
- php 数组 array_values () array_key()
<?php // array_unique($array) 去除重复 // array_unshif()向数组的顶部追加函数 // array_shif($a,"ss")向数 ...