2

マウスが押されている間にセレクターを繰り返す必要があるため、NSButton をサブクラス化しています。

私はこのようにやっています:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
        [self setBezelStyle:NSBezelBorder];
        PotRightIsDown = NO;
    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    // Drawing code here.
}

- (void)mouseDown:(NSEvent *)theEvent;
{    
    NSLog(@"pot right mouse down");
    PotRightIsDown = YES;
    holdDownTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sendCommand) userInfo:nil repeats:YES];
}

- (void)mouseUp:(NSEvent *)theEvent;
{
    NSLog(@"pot right mouse up");
    PotRightIsDown = NO;
}

-(void)sendCommand
{
    if (PotRightIsDown)
    {
        NSLog(@"run the stuff here");
    }
    else 
    {
        [holdDownTimer invalidate];
    }
}

チャンピオンのように機能し、100ms ごとにコマンドを送信します。

IB のウィンドウで、Bevel Button をウィンドウにドラッグし、そのクラスをこのサブクラスに設定しました。アプリケーションを実行すると、ボタンは表示されませんが、機能します。これは、サブクラスに空の drawRect 関数があるためだと思います。

このサブクラス化されたボタンをベベル ボタンのように見せるにはどうすればよいですか?

ありがとう、
ステートフル

4

1 に答える 1

3

特定のサブクラス メソッドに機能を追加しない場合は、それを完全に実装することを避けることができます。これにより、スーパークラスがデフォルトの動作を提供できるようになります。

あるいは(私の@Carl Norumが指摘したように)次を使用して明示的に行うことができます:

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];
}

しかし、それは少し無意味です。

于 2012-05-04T08:14:09.877 に答える