调用一次计时器方法:

//不重复,只调用一次。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使用的更多相关文章

  1. 蜗牛爱课 -- iOS 设计模式之模板模式

    1 前言 模板方法模式是面向对象软件设计中一种非常简单的设计模式.其基本思想是在抽象类的一个方法定义“标准”算法.在这个方法中调用的基本操作由子类重载予以实现.这个方法成为“模板”.因为方法定义的算法 ...

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

    调用一次计时器方法: myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scro ...

  3. 【转】iOS中定时器NSTimer的使用

    原文网址:http://www.cnblogs.com/zhulin/archive/2012/02/02/2335866.html 1.初始化 + (NSTimer *)timerWithTimeI ...

  4. iOS中定时器NSTimer的使用-备用

    1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...

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

    原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...

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

    原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...

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

      一.只调用一次计时器方法: //不重复,只调用一次.timer运行一次就会自动停止运行 myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5  ...

  8. ios 中定时器:NSTimer, CADisplayLink, GCD

    #import "ViewController.h" #import "RunloopViewController.h" @interface ViewCont ...

  9. 蜗牛爱课 -- iOS 设置UIButton的字体的大小、显示位置、大小

    /设置按钮上的自体的大小 //[btn setFont: [UIFont systemFontSize: 14.0]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法 ...

随机推荐

  1. C#关于ref的用法(多个实参值的传递)

    按照C#默认的按值调用参数的传递机制,不能刻编写出一个方法来实现两个int类型的值交换,因为一个方法只能对应一个返回值,如何实现将两个交换的值传递回去,这里我将用到的是ref修饰符. 使用ref的单值 ...

  2. NSArray数组的学习总结

    1.不可变数组NSArray NSArray是不可变的,而且只能储存Object-c对象.另外,数组的最后一个元素一定是nil,表示结束. 注:这些集合类只能收集cocoa对象(NSOjbect对象) ...

  3. HDU 1088 - Write a simple HTML Browser

    直接看sample input = = 又一道模拟. #include <iostream> #include <string> #include <cstdio> ...

  4. (原)matlab中使用mex编译多个cpp文件

    以前一直是mex一个文件.刚才需要编译多个文件(如a.cpp调用b.cpp的函数,b.cpp调用c.cpp的函数).如果只是mex a.cpp,提示函数找不到函数. 突然想到mex c.cpp b.c ...

  5. 从汇编看c++多重继承中this指针的变化

    先来看一下下面的c++源码: #include <iostream> using namespace std; class X { public: virtual void print1( ...

  6. python中的星号*、**的意义

    我们都知道,定义一个函数,当传入的参数个数未知时就可以使用*来表示. 单引号*: def test(*args): if len(args) >= 4: print(arg[3]) test(1 ...

  7. asp.net基础学习笔记

    原文地址:http://blog.csdn.net/oxoxzhu/article/details/8652530 1.概论 浏览器-服务器 B/S 浏览的      浏览器和服务器之间的交互,形成上 ...

  8. 这是一个hibernate 联合主键的例子

    package com.bird.entity; import java.io.Serializable; import javax.persistence.Entity; import javax. ...

  9. pyspark 写 logistic regression

    import random as rd import math class LogisticRegressionPySpark: def __init__(self,MaxItr=100,eps=0. ...

  10. hdu 2157 How many ways_ 矩阵快速幂

    题意:略 直接矩阵乘法就行了 #include <iostream> #include<cstdio> #include<cstring> using namesp ...