【转】IOS中定时器NSTimer的开启与关闭
原文网址:http://blog.csdn.net/enuola/article/details/8099461
调用一次计时器方法:
- myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scrollTimer) userInfo:nil repeats:NO];
- //不重复,只调用一次。timer运行一次就会自动停止运行
重复调用计时器方法:
- timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(function:) userInfo:nil repeats:YES];
- //每1秒运行一次function方法。
注意:将计数器的repeats设置为YES的时候,self的引用计数会加1。因此可能会导致self(即viewController)不能release,所以,必须在viewWillAppear的时候,将计数器timer停止,否则可能会导致内存泄露。
停止timer的运行,但这个是永久的停止:(注意:停止后,一定要将timer赋空,否则还是没有释放。不信?你自己试试~)
- //取消定时器
- [timer invalidate];
- timer = nil;
要想实现:先停止,然后再某种情况下再次开启运行timer,可以使用下面的方法:
首先关闭定时器不能使用上面的方法,应该使用下面的方法:
- //关闭定时器
- [myTimer setFireDate:[NSDate distantFuture]];
然后就可以使用下面的方法再此开启这个timer了:
- //开启定时器
- [myTimer setFireDate:[NSDate distantPast]];
例子:比如,在页面消失的时候关闭定时器,然后等页面再次打开的时候,又开启定时器。
(主要是为了防止它在后台运行,暂用CPU)可以使用下面的代码实现:
- //页面将要进入前台,开启定时器
- -(void)viewWillAppear:(BOOL)animated
- {
- //开启定时器
- [scrollView.myTimer setFireDate:[NSDate distantPast]];
- }
- //页面消失,进入后台不显示该页面,关闭定时器
- -(void)viewDidDisappear:(BOOL)animated
- {
- //关闭定时器
- [scrollView.myTimer setFireDate:[NSDate distantFuture]];
- }
OK,搞定。
【转】IOS中定时器NSTimer的开启与关闭的更多相关文章
- IOS中定时器NSTimer的开启与关闭
调用一次计时器方法: myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scro ...
- 【转】 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的使用
原文网址: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, CADisplayLink, GCD
#import "ViewController.h" #import "RunloopViewController.h" @interface ViewCont ...
- iOS 中的 NSTimer
iOS 中的 NSTimer NSTimer fire 我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: @interface Detail ...
- IOS中的NSTimer定时器详解
/* 在IOS中有多种定时器,这里我对NSTimer定时器做了一个简单的介绍.如果你是小白,你可能会从这篇文章中学习到一些知识,如果你是大牛,请别吝啬你的评论,指出我的不足,你的质疑是对我最大的帮助. ...
随机推荐
- hdu 3092 Least common multiple
思路: 容易知道,分解成素数的lcm肯定是最大的,因为假设分解成2个合数,设定x为他们的 最大公约数, 那么他们的最小公倍数就要减少x倍了 然后如果是素数之间的最小公倍数,那么就只是他们的乘积,同样的 ...
- facebook design question 总结
http://blog.csdn.net/sigh1988/article/details/9790337 这里原帖地址: http://www.mitbbs.com/article_t/JobHun ...
- 问题:-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "BlueView" nib but the view outlet was not set.
问题:-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "BlueView" nib but the vie ...
- online judge 提交代码应该注意的事项
首先,eclipse工程上出现红色的惊叹号,这个时候一般是工程的参考library或者build path的jar文件或者库文件缺失,可以右键工程,打开properties,点击 java build ...
- mq_setattr
NAME mq_setattr - 设置消息队列的属性(REALTIME) SYNOPSIS #include <mqueue.h> int mq_setattr(mqd_t mqdes, ...
- Tail-chaining(末尾连锁)中断说明
[转载]http://lxdawn.blog.163.com/blog/static/173620990201273111337204/ Tail-chaining是指一个中断退出至下一个中断进入这段 ...
- Why you have so few friends?
Why you have so few friends?十个原因告诉你:为什么你的朋友那么少1. You Complain A Lot 你总是抱怨 If you’re constantly compl ...
- C# 访问USB(HID)设备
原文:C# 访问USB(HID)设备 二话不说,直接给代码,如果您真想做这方面的东西,还是稍微研究下,没有现成的好类用,就需要自己了解其原理 //引用空间 using System; using Sy ...
- ZOJ Problem Set - 3865 Superbot (bfs)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477 大牛博客:http://www.cnblogs.com/kylehz/p ...
- Python Mongo操作
# -*- coding: utf-8 -*- ''' Python Mongo操作Demo Done: ''' from pymongo import MongoClient conn = None ...