1

問題について助けが必要です。

目標
ビューを読み込んだ後、NSTimers を使用していくつかのずらしたアニメーション イベントを発生させる iOS ブック アプリをまとめています。これを行うのに役立つMethodCallerWithTimerクラスを作成しました(コードは下部にあります)。

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

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

私の問題
複数のMethodCallerWithTimerインスタンスを作成し、ビューをロードしてそれらをすべて開始すると、最初のイベントしか発生しません。他のどのタイマーも、ターゲット メソッドを呼び出しません。NSRunLoop に何を求めているのか、または同様のことを理解していないと思われます。

何かご意見は?

ここに私のMethodCallerWithTimerクラスがあります:

@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

そして実装:

#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

1 に答える 1

4

これらは、遅延後の 1 回限りの発火のように見えます。次のようなものを使用することを検討しましたか:

[myObject performSelector:@selector(playEmory) withObject:nil afterDelay:myDelay];

ルーチンmyObjectのインスタンスはどこにあり、呼び出しを行う前に OS に待機させたい秒数は?playEmorymyDelayfloat

このフレーバーの詳細については、performSelector こちらをご覧ください。

于 2011-04-01T21:11:46.020 に答える