マウスが押されている間にセレクターを繰り返す必要があるため、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 関数があるためだと思います。
このサブクラス化されたボタンをベベル ボタンのように見せるにはどうすればよいですか?
ありがとう、
ステートフル