2012年7月10日火曜日

[iOS]簡単なタイマー処理

- (void)startTimer
{
    //タイマーの開始
    ctimer = [NSTimer scheduledTimerWithTimeInterval:0.25
                                              target:self
                                            selector:@selector(timerDidEnd)
                                            userInfo:@"userInfoString";
                                             repeats:YES];
}

- (void)timerDidEnd
{
    //タイマーが1回終了した

    //userInfoで値を取得できる
    NSString *string = [timerDidEnd userInfo];
}

- (void)stopTimer
{
    //タイマーの停止
    if (ctimer) {
        if ([ctimer isValid]) {
            [ctimer invalidate];
            //停止時にはnilにすること
            ctimer = nil;
        }
        
    }
}

- (void)dealloc
{
    [super dealloc];
    //タイマーの停止
    if (ctimer) {
        if ([ctimer isValid]) {
            [ctimer invalidate];
            //停止時にはnilにすること
            ctimer = nil;
        }
    }
}

0 件のコメント:

コメントを投稿