macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

timerWithTimeInterval:invocation:repeats:

タイマーを秒、起動オブジェクト、繰り返しで作って返します
+(NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds:
                 invocation:(NSInvocation *)invocation:
                 repeats:(BOOL)repeats:

解説

タイマーを作って返します。
secondsで指定した時間(秒)が過ぎた後、ターゲットに起動のメッセージを送ります。
repeatsをYESにすると、タイマーは繰り返して実行されます。NOをセットすると1回だけメッセージを送ります。
ランループに登録されるモードがNSEventTrackingRunLoopModeならユーザーがイベントを送信しているときにタイマーは実行されます。
NSModalPanelRunLoopModeならモーダルパネルの時にタイマーは実行されます。

返り値

( NSTimer * )

タイマー

引数

( NSTimeInterval )seconds

タイマーが呼び出される間隔(秒)

( NSInvocation * )invocation

起動オブジェクト

( BOOL )repeats

繰り返すかYES/NO

クラス

NSTimer

Class Methods

使用可能

10.0

参照

例文

#import "MyObject.h"
@implementation MyObject

NSTimer *timer;

- (IBAction)myAction:(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:0.1
                        invocation:	invocation
                        repeats:YES];


	//念のためモーダルが動いていたら終了
	[NSApp stopModal];
	//現在の実行ループに追加
	[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];
	//シートで表示
	
	[NSApp beginSheet:	myPanel
					modalForWindow: myWindow
					modalDelegate:self
					didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
					contextInfo:nil
					];
	NSLog(@"open sheet");
	
}
- (IBAction)fire:(id)sender
{

}
//シートについているボタン 押したらモーダル終了
- (IBAction)stop:(id)sender
{
	[NSApp endSheet:myPanel];
}
-(void) timerControl{
	NSLog(@"...");
}
//閉じる時の処理
- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
[sheet orderOut:self];
NSLog(@"close sheet");
}

@end