2011-08-17 12 views
0

私は別で、その後....それは0.2秒ごとに呼び出す必要があります10秒間複数のNSTimerを扱うには?

...機能は、私はこのようにように、このメソッドを呼び出したいのOnTimer

-(void)onTimer { 
     * Some Operation * 
} 

言うことができます持っています10秒間、このメソッドを呼び出す持続時間を長くする必要があります。これを行うと、高速モードからの処理が遅くなり、最後に停止します。

ご案内しています。

答えて

1

私は、これが実現するのはかなり簡単だと思います2タイマー。 .mファイルで

float intervalYouWant; 
NSTimer * timer1; 
NSTimer * timer2; 

、最初のタイマーごとに10秒をキャンセルし、新しい時間間隔でそれを再起動する必要があり

- (void)viewDidLoad; { 
    intervalYouWant = 0.2; 
    timer1 = [NSTimer scheduledTimerWithTimeInterval:intervalYouWant target:self selector:@selector(methodForTimer1) userInfo:nil repeats:YES]; 
    timer2 = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(changeTimer1) userInfo:nil repeats:YES]; 
} 

- (void)changeTimer1; { 
    [timer1 invalidate]; 
    timer1 = nil; 
    intervalYouWant += amountYouWantToAdd; 
    timer1 = [NSTimer scheduledTimerWithTimeInterval:intervalYouWant target:self selector:@selector(methodForTimer1) userInfo:nil repeats:YES]; 
} 

:.hファイルでは、2つのタイマーを宣言。 deallocメソッドでタイマーを無効にすることを忘れないでください。希望が助けてくれる!

0

スタートノンリピートモード時のタイマmemory..Soの構文エラーから書か

float interval = 0.2; //global variable 

[NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(timerSelector:) userInfo:nil repeats:NO]; 

.......... 
.......... 

-(void) timerSelector:(NSTimer *)timer{ 
    static float timeConsumed = 0.0; 
    //do your task which you want to do here 
    ............ 
    ............ 

// in the end 
    if(timeConsumed > 10.0){ 
    interval = 0.5; //increase the interval so it decrease the speed.. 
    }else if(timeConsumed > 20.0){ 
    interval = 1.0; 
    }... go on like this until you stop it.. 

    timeConsumed += interval; 
    [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(timerSelector:) userInfo:nil repeats:NO]; 
} 

このことができますyourself..hopeをpossible..Correct ..

+0

私はあなたのコードfloat intervalYouWant = 0.2でこれらの値を入力しました。 float amountYouWantToAdd = 0.5;それはうまく動作していない...それは無限の時間のために0.2スピードで走っている.....それは増加する時間ではなく、それは数時間後に停止しません。私の仕事は、起動時にそのメソッドを速く呼び出すことです...特定のペリオイドの後に遅くなります...カジノのスクロールのように... – Tariq

+0

あなたのコードであなたの質問を編集してください。また、変数間隔を設定する場所... timeConsumedは静的変数 – Krishnabhadra

関連する問題