我将 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];
}
}
像冠军一样工作,每 100 毫秒发送一次命令。
在 IB 的窗口中,我将一个 Bevel Button 拖到窗口上并将其类设置为该子类。当我运行该应用程序时,该按钮是不可见的,但它可以工作。我猜这是因为我在子类中有一个空的drawRect函数。
如何使这个子类按钮看起来像斜角按钮?
谢谢,
有状态
最佳答案
如果您没有向特定子类方法添加任何功能,那么您可以完全避免实现它,这将允许父类(super class)提供默认行为。
或者(正如我的@Carl Norum 所指出的),您可以使用以下方式明确执行此操作:
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
}
但这有点毫无意义。
关于cocoa - 子类化 NSButton,需要使其看起来像常规按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10443116/