1. 程式人生 > >NSTimer的使用以及NSTimer暫停和繼續

NSTimer的使用以及NSTimer暫停和繼續

NSTimer的使用方法

1、初始化

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

注:不用scheduled方式初始化的,需要手動addTimer:forMode: 將timer新增到一個runloop中。

  而scheduled的初始化方法將以預設mode直接新增到當前的runloop中.

 

scheduledTimerWithTimeInterval:(NSTimeInterval)seconds  

預訂一個Timer,設定一個時間間隔。

表示輸入一個時間間隔物件,以秒為單位,一個>0的浮點型別的值,如果該值<0,系統會預設為0.1

 target:(id)aTarget

表示傳送的物件,如self

 selector:(SEL)aSelector

方法選擇器,在時間間隔內,選擇呼叫一個例項方法

userInfo:(id)userInfo

此引數可以為nil,當定時器失效時,由你指定的物件保留和釋放該定時器。

repeats:(BOOL)yesOrNo

當YES時,定時器會不斷迴圈直至失效或被釋放,當NO時,定時器會迴圈傳送一次就失效。

invocation:(NSInvocation *)invocation



舉例:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];

或

NSTimer *myTimer = [NSTimertimerWithTimeInterval:3.0 target:selfselector:@selector(timerFired:)userInfo:nilrepeats:NO];

[[NSRunLoopcurrentRunLoop]addTimer:myTimerforMode:NSDefaultRunLoopMode];

 

2、觸發(啟動)

當定時器建立完(不用scheduled的,新增到runloop中後,該定時器將在初始化時指定的timeInterval秒後自動觸發。



可以使用-(void)fire;方法來立即觸發該定時器;

注:You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.

在重複執行的定時器中呼叫此方法後立即觸發該定時器,但不會中斷其之前的執行計劃;

在不重複執行的定時器中呼叫此方法,立即觸發後,就會使這個定時器失效。

 

3、停止

- (void)invalidate;

這個是唯一一個可以將計時器從runloop中移出的方法。

 

注:

NSTimer可以精確到50-100毫秒.

NSTimer不是絕對準確的,而且中間耗時或阻塞錯過下一個點,那麼下一個點就pass過去了.

原文地址:http://61party.iteye.com/blog/1195165

 

為NSTimer新增一個分類

#import "TFTimer.h"


#import <Foundation/Foundation.h>



@interface NSTimer (TFAddition)



-(void)pauseTimer;
-(void)resumeTimer;

@end




#import "TFTimer.h"


@implementation NSTimer (TFAddition)


-(void)pauseTimer{

if (![self isValid]) {
return ;
}

[self setFireDate:[NSDate distantFuture]]; //如果給我一個期限,我希望是4001-01-01 00:00:00 +0000


}


-(void)resumeTimer{

if (![self isValid]) {
return ;
}

//[self setFireDate:[NSDate dateWithTimeIntervalSinceNow:0]];
[self setFireDate:[NSDate date]];

}

@end

 

 

相關推薦

NSTimer的使用以及NSTimer暫停繼續

NSTimer的使用方法 1、初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userIn

CocosCreator原生平臺退出遊戲,暫停繼續

cos osc 運行 暫停 res 退出 life 官方 想要 原生平臺退出遊戲,方法為:cc.director.end();官方解釋:End the life of director in the next frame暫停遊戲,方法: cc.director.pause(

iOS開發—執行緒的模擬暫停繼續操作

表檢視開啟執行緒下載遠端的網路介面,滾動頁面時勢必會有影響,降低使用者的體驗。針對這種情況,當用戶滾動螢幕的時候,暫停佇列;使用者停止滾動的時候,繼續恢復佇列。接下來通過一個案例,演示如何暫停和繼續操作,具體內容如下: (1)新建一個SingleViewApplicatio

關於安卓如何實現計時器chronometer的暫停繼續計時功能

package com.book.b525; import java.text.ChoiceFormat; import android.app.Activity; import android.app.ActionBar; import android.app.AlertDialog; import and

iOS --- 如何暫停繼續CALayer上的動畫

本文主要介紹瞭如何暫停和繼續CALayer的動畫. 首先來看CALayer. /** The base layer class. **/ @interface CALayer : NSObject <NSCoding, CAMediaTiming&

iOS--------如何在ios中運用core animation暫停繼續動畫

核心程式碼如下 //暫停layer上面的動畫 - (void)pauseLayer:(CALayer*)layer { CFTimeInterval pausedTime = [laye

使用service的startservice bindservice方法實現音樂的播放、暫停繼續

meidiaPlay使用簡單步驟 1、create方法 第一種: 載入本地音訊檔案 MediaPlayer.create(this, R.raw.test);(我在android專案的res目錄下新建raw目錄 並放置test.mp3檔案)

Android中定時器TimerTimerTask的啟動,停止,暫停繼續等操作例項

下面是一個在Android中使用定時器Timer和TimerTask的啟動,停止,暫停,繼續等操作的demo。 需要注意的問題主要有兩點: 1、Timer和TimerTask在呼叫cancel()取消後不能再執行 schedule語句,否則提示出錯,提示如下:D/Andro

轉:掃盲 DNS 原理,以及“域名劫持”“域名欺騙/域名汙染”

下載工具 位置 如何 域名劫持 入侵 簡單的 國內 log 配置 [轉]掃盲 DNS 原理,以及“域名劫持”和“域名欺騙/域名汙染” 掃盲 DNS 原理,以及“域名劫持”和“域名欺騙/域名汙染” 文章目錄 DNS 是啥? DNS 有啥用? 域名的結構是咋樣滴

bash簡介以及bash應用命令

linuxbash及其特性:shell:外殼GUI:Gnome,KDE,xfceCLI: sh,csh.ksh,bash,tcsh,zsh程序:只有一個進程:可以有多個進程:在每個進程看來,當前主機上只存在內核和當前進程。進程是程序的副本,進程是程序執行實例。用戶工作環境:bash: #管理

(一)關於spring security的簡要介紹以及相關配置jar包認識

重要 force cnblogs control 自定義攔截器 compute 編寫 -- 靈活 Spring Security是一個能夠為基於Spring的企業應用系統提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring

Unity3d-WWW實現圖片資源顯示以及保存本地加載

editor call clas amp private out .text sset bytes 本文固定連接:http://blog.csdn.net/u013108312/article/details/52712844 WWW實現圖片資源顯示以及保存和本地加

python-繼承以及繼承問題多態

alt 字符 面向 什麽 get 避免 類對象 小結 base 繼承# 面向對象的三大特性之一:繼承# 繼承:至少有兩個類:什麽是什麽的關系,為了避免幾個類之間有相同的代碼# 父類:Animal# 子類:Dog Person# 動物 示例 # python中有兩種類:經典類

PHP關於VC11,VC9,VC6以及Thread SafeNon Thread Safe版本選擇的問題

pac nts isa 新版 unix 每一個 inux 兼容 suse 現在PHP官網上下載PHP安裝包都有VC11或VC9的字樣,這是什麽含義,我們應該下載哪種安裝包更好呢? 其實PHP官網給出了答案: VC6版本是使用Visual Studio 6編譯器編譯的,如果你

2.2.6 掛起繼續執行

一次 pack 現在 中斷線程 main 新建 cep pub 狀態 package 第二章.新建線程;/** * Created by zzq on 2018/1/17. */public class 線程中斷 { public static void main(S

Android 音視頻深入 十三 OpenSL ES 制作音樂播放器,能暫停調整音量(附源碼下載)

音視頻 OpenSL ES 項目地址https://github.com/979451341/OpenSLAudio OpenSL ES 是基於NDK也就是c語言的底層開發音頻的公開API,通過使用它能夠做到標準化, 高性能,低響應時間的音頻功能實現方法。 這次是使用OpenSL ES來做一個音樂播

蘋果產品時間發布表統計(iPhone、iPad),以及32位64位機的說明

bsp 產品 13.10 上市 pos min ipad mini 時間排序 手機 之前因為某些原因,需要對apple家族的手機和pad產品做一個上市時間排序,以及分析分別是哪種CPU機型 總結如下: iPad家族: 1、iPad     - 2010.1.27發布 2、

文件流的使用以及序列化反序列化的方法使用

不能 引入 創建 exist files 文件流對象 des file 列表 步驟一:引入IO文件和二進制格式序列化文件using System.IO;using System.Runtime.Serialization.Formatters.Binary;步驟二:將圖書對

關於盒子模型以及塊元素行內元素

內邊距 頁面元素 adding 分享圖片 alt pad image con bsp   盒子模型,盒子模型算是頁面比較經典的一個概念了,所有的頁面元素和div都能稱之為一個盒子,每個盒子都具備:內容(content),填充(padding 也稱之為內邊距),邊框(bor

union, enum, and struct, 以及結構填充位字段實現。

表示 bits 聯合 follow ++ alignment necessary only lds Table 4-9 Compiler storage of data objects by byte alignment TypeBytesAlignmen