NSTimerクラス
解説
継承 | NSObject |
準拠 | NSObject (NSObject) |
フレームワーク | /System/Library/Frameworks/Foundation.framework |
使用可能 | OS X 10.0以降 |
iOS 2.0以降 | |
定義 | NSTimer.h |
概要
定期的に呼び出す場合に使用するタイマーオブジェクトのクラスです。一定の時間をおいて一度だけ呼び出すことも、繰り返し呼び出すこともできます。
このクラスはCore FoundationのCFRunLoopTimerRefとトールフリーブリッジです。
このクラスはサブクラス化すべきではありません。
作成
起動オブジェクトと起動間隔からタイマーを作成するには(+ scheduledTimerWithTimeInterval:invocation:repeats: )メソッドを使用します。repeatsにYESが渡されると繰り返します。このメソッドはオブジェクトを作成するとすぐに動き出します。
NSTimer *timer = nil; -(void) timerControl:(id)sender{ NSLog(@"%s ...",__FUNCTION__); } - (IBAction)method001:(id)sender { //メソッドシグネチャ NSMethodSignature* aSignature ; //起動オブジェクト NSInvocation *invocation ; //セレクタ SEL aSelector = @selector( timerControl: ); //メソッドシグネチャ aSignature = [ self methodSignatureForSelector:aSelector ]; //起動オブジェクトを作る invocation = [ NSInvocation invocationWithMethodSignature:aSignature ]; //ターゲットをセット [ invocation setTarget: self ]; //セレクタをセット [ invocation setSelector: aSelector ]; NSLog(@"%s start",__FUNCTION__); //タイマースタート timer = [NSTimer scheduledTimerWithTimeInterval:1.0 invocation:invocation repeats:YES]; }
セレクタと起動間隔からタイマーを作成するには(+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: )メソッドを使用します。repeatsにYESが渡されると繰り返します。このメソッドはオブジェクトを作成するとすぐに動き出します。
起動オブジェクトとタイマーを作成するには(+ timerWithTimeInterval:invocation:repeats: )メソッドを使用します。RunLoopにタイマーを追加する必要があります。repeatsにYESが渡されると繰り返します。このメソッドはオブジェクトを作成するとすぐに動き出します。追加情報を付加することができるメソッド(+ timerWithTimeInterval:target:selector:userInfo:repeats: )もあります。
- (IBAction)method005:(id)sender { //メソッドシグネチャ NSMethodSignature* aSignature ; //起動オブジェクト NSInvocation *invocation ; //セレクタ SEL aSelector = @selector( timerControl: ); //メソッドシグネチャ aSignature = [ self methodSignatureForSelector:aSelector ]; //起動オブジェクトを作る invocation = [ NSInvocation invocationWithMethodSignature:aSignature ]; //ターゲットをセット [ invocation setTarget: self ]; //セレクタをセット [ invocation setSelector: aSelector ]; timer = [NSTimer timerWithTimeInterval:1.0 invocation: invocation repeats:YES]; NSRunLoop *runloop = [NSRunLoop currentRunLoop]; NSLog(@"%s timerWithTimeInterval:invocation:repeats",__FUNCTION__); [runloop addTimer:timer forMode:NSRunLoopCommonModes]; [runloop addTimer:timer forMode:UITrackingRunLoopMode]; }
allocで作成して起動日時と起動間隔ターゲット、セレクタ、追加情報でタイマーを初期化するには(– initWithFireDate:interval:target:selector:userInfo:repeats: )メソッドを使用します。RunLoopにタイマーを追加する必要があります。repeatsにYESが渡されると繰り返します。このメソッドはオブジェクトを作成するとすぐに動き出します。
タイマーの停止
タイマーを停止するには(– invalidate)メソッドを送信します。
タイマーについての情報タイマーが有効かどうかを調べるには(– isValid)メソッドを使用します。
タイマーが起動する時刻をセットするメソッドが(– setFireDate: )で、時刻を取得するメソッドが(– fireDate)です。
繰り返し起動する時間間隔を取得するには(– timeInterval)メソッドを使用します。
追加情報を取得するには(– userInfo)メソッドを使用します。追加情報は辞書NSDictionaryで返されます。
適合するプロトコル
メソッド
タイマーの作成
+ scheduledTimerWithTimeInterval:invocation:repeats:
+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
+ timerWithTimeInterval:invocation:repeats:
+ timerWithTimeInterval:target:selector:userInfo:repeats:
– initWithFireDate:interval:target:selector:userInfo:repeats:
タイマーの起動
タイマーのストップ
タイマーのついての情報
– isValid
– fireDate
– setFireDate:
– timeInterval
– userInfo