2
我是NSButton的子类,因为我需要在按住鼠标的同时重复选择器。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的窗口中,我将一个斜角按钮拖到窗口上,并将其设置为该子类的类。当我运行应用程序时,该按钮是不可见的,但它可以工作。我猜这是因为我在子类中有一个空的drawRect函数。
如何使这个子类按钮看起来像一个斜角按钮?
谢谢
有状态
'[super drawRect:dirtyRect]'? –
卡尔是正确的你需要用'-drawRect'方法编写它。但是我看到你使用'-mouseDown'和'-mouseUp'时按下左键不正确。如果你想正确的你需要使用'-rightMouseDown'和'-rightMouseUp'。 –
那很简单。谢谢你的提示,Carl。请添加它作为答案,我会选中它。贾斯汀,放下鼠标是正确的。我只是把它叫做PotRight,因为当你按住它时,它将命令从一个串行端口发送到一个控制器,该控制器将该命令解释为电位器顺时针旋转,以控制我所控制的事物。 – Stateful