2011-04-01 1 views
1

問題の解決に役立つ必要があります。iOS上で複数のNSTimersを使用する - 1つのタイマーだけが起動する

ゴール
私はビューをロードした後、いくつかの千鳥アニメーションイベントをオフに火災にNSTimersを使用したiOSブックアプリを一緒に入れています。私はMethodCallerWithTimerクラスを作成しました(下のコード)。

私の解決策これまで
私はMethodCallerWithTimerクラスを使用すると、私は(それが本のページです)私のUIViewControllerサブクラスオブジェクトとしてobjectOwningMethodを割り当て、そのクラスのインスタンスメソッドとして、メソッド。ここで私は割り当てる方法の一例である - 非常に単純に画面上のいくつかのアートワークをオンにする:

- (void) playEmory { 
    [emoryRedArt setHidden:NO]; 
} 

私の問題
私はビューをロードし、それらのすべてを開始し、その後、複数のMethodCallerWithTimerインスタンスを作成すると、私は初めての出来事を起こすだけです。他のタイマーのいずれもターゲットメソッドを呼び出すことはありません。 NSRunLoopに何か似たようなことを聞​​いているのか分かりません。

どのような考えですか?

@interface MethodCallerWithTimer : NSObject { 
    NSTimer * timer; 
    NSInvocation * methodInvocationObject; 
    NSNumber * timeLengthInMS; 
} 

- (id) initWithObject: (id) objectOwningMethod AndMethodToCall: (SEL) method; 
- (void) setTime: (int) milliseconds; 
- (void) startTimer; 
- (void) cancelTimer; 

@end 

と実装:ここ

は私のMethodCallerWithTimerクラスです

#import "MethodCallerWithTimer.h" 

@implementation MethodCallerWithTimer 

- (id) initWithObject: (id) objectOwningMethod AndMethodToCall: (SEL) method { 
    NSMethodSignature * methSig = [[objectOwningMethod class] instanceMethodSignatureForSelector:method]; 
    methodInvocationObject = [NSInvocation invocationWithMethodSignature:methSig]; 
    [methodInvocationObject setTarget:objectOwningMethod]; 
    [methodInvocationObject setSelector:method]; 
    [methSig release]; 
    return [super init]; 
} 
- (void) setTime: (int) milliseconds { 
    timeLengthInMS = [[NSNumber alloc] initWithInt:milliseconds]; 
} 
- (void) startTimer { 
    timer = [NSTimer scheduledTimerWithTimeInterval:([timeLengthInMS longValue]*0.001) invocation:methodInvocationObject repeats:NO]; 
} 
- (void) cancelTimer { 
    [timer invalidate]; 
} 
-(void) dealloc { 
    [timer release]; 
    [methodInvocationObject release]; 
    [timeLengthInMS release]; 
    [super dealloc]; 
} 

@end 

答えて

4

これらは、遅延の後に一回発射のように見えます。

myObjectplayEmoryルーチンを持つインスタンスであり、 myDelayあなたはOSが呼び出しを行う前に待機する秒の floatある
[myObject performSelector:@selector(playEmory) withObject:nil afterDelay:myDelay]; 

:あなたはのようなものを使用して考えがありますか?

このフレーバーについて詳しくは、performSelectorhereをご覧ください。

+0

GREAT suggestion、fbrereto。ありがとう。それは私のために働いた。私は以前の実装がなぜそうでなかったのか不思議ですが、これでうまくいくはずです。 – mikedishere

関連する問題