2
-(void)setX:(int)x andY:(int)y andObject:(Sprite*)obj
{
    [obj setPosition:CGPointMake(x,y)];
}

ここで、次のタイマーを使用して上記のメソッドを呼び出したいと思います。

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

ここに何を設定しますか?

引数を渡すには? (私の知る限り、セレクターは呼び出すメソッドのみを指定します)

4

7 に答える 7

7

+[NSTimer scheduledTimerWithTimeInterval:invocation:repeats:]代わりに使用する必要があります。デフォルトでは、タイマーを起動するために使用されるセレクターは 1 つのパラメーターを取ります。それ以外のものが必要な場合は、代わりにタイマーが使用するNSInvocationオブジェクトを作成する必要があります。

于 2009-10-06T18:09:22.987 に答える
6

メソッドを呼び出すために使用したいかなり複雑な引数のセットがある場合は、引数を構成を保持するものにキャプチャし、その構成に基づいて必要なことを何でも実行できるようにすることをお勧めします...

このようなインターフェースを持つもの:

PositionSetter.h:

@interface  PositionSetter : NSObject
{
    NSInteger x;
    NSInteger y;
    Sprite *target;
}

+ positionSetterWithX: (NSInteger) xPos y: (NSInteger) yPos sprite: (Sprite *) aSprite; 

- (void) applyPosition;
@end

PositionSetter.m:

@interface PositionSetter()
@property(readwrite, nonatomic) NSInteger x;
@property(readwrite, nonatomic) NSInteger y;
@property(readwrite, nonatomic, retain) Sprite *target;
@end

@implementation PositionSetter
@synthesize x, y, target;

+ positionSetterWithX: (NSInteger) xPos y: (NSInteger) yPos sprite: (Sprite *) aSprite; 
{
    PositionSetter *positionSetter = [PositionSetter new];
    positionSetter.x = xPos;
    positionSetter.y = yPos;
    positionSetter.target = aSprite;
    return [positionSetter autorelease];
}

- (void) applyPosition;
{
    [self.target setPosition:CGPointMake(self.x,self.y)];
}
@end

使い方はとても簡単です:

positionSetter = [PositionSetter positionSetterWithX: 42 y: 21 sprite: mySprite];
[positionSetter performSelector: @selector(applyPosition) withObject: nil afterDelay: 1.0];

少しコードが増えますが、結果として得られる実装は十分に高速です。おそらく NSInvocation よりも高速ですが、これが描画を引き起こすことを考えると、無関係であるほど高速であり、はるかに柔軟です。上記をリファクタリングして、たとえば CoreAnimation を駆動することが簡単にわかりました。

于 2009-10-06T18:24:47.603 に答える
5

Matt Ballの回答からコピー:

    - (void)startMyTimer {
        /* ... Some stuff ... */
        NSDictionary *userDict;
        userDict = [NSDictionary dictionaryWithObjectsAndKeys:someValue,
                                                              @"value1",
                                                              someOtherValue,
                                                              @"value2", nil];

        [NSTimer scheduledTimerWithTimeInterval:0.1
                                         target:self
                                       selector:@selector(callMyMethod:)
                                       userInfo:userDict
                                        repeats:YES];
}
    - (void)callMyMethod:(NSTimer *)theTimer {
        NSString *value1 = [[theTimer userInfo] objectForKey:@"value1"];
        NSString *value2 = [[theTimer userInfo] objectForKey:@"value2"];
        [self myMethod:value1 setValue2:value2];
    }
于 2009-10-06T19:14:20.997 に答える
2

ターゲット アクション タイマーを使用する場合、タイマーで任意のメソッドを直接呼び出すことはできません。タイマーのアクションには、非常に具体的な署名が必要です。userinfo ディクショナリに追加のデータを渡し、タイマーのアクションで最終的に必要なメソッドを呼び出すか、Dave が言ったように呼び出しフォームを使用できます。個人的には、NSInvocations は煩わしく、実際に NSInvocations を設定すると、仲介メソッドを記述するよりも多くのコードが必要になるため、通常は前者を使用します。

于 2009-10-06T18:12:47.740 に答える
1

NSDictionary* またはその他のオブジェクトを userInfo として渡し、その中に引数を入れることができます。

于 2009-10-06T18:08:14.363 に答える
0

それらの引数を使用して辞書を作成し、タイマーuserinfoを使用してその辞書を渡します。それはあなたの問題を解決します

于 2012-08-27T11:19:33.850 に答える