cocoa - 子类化 NSButton,需要使其看起来像常规按钮

标签 cocoa 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];
    }
}

像冠军一样工作,每 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/

相关文章:

macos - NSDocument获取实际保存路径?

objective-c - 修改 NSScroller 的长度

cocoa - 轮廓 View :dataCellForTableColumn:item: has strange side effect

objective-c - 是否可以在 Objective-C 中创建 "Block"对象的类别

macos - 右侧带有向下三角形的 NSButton

objective-c - 简单的 NSButton 隐藏另一个 NSButton

cocoa - NSButton-点击时显示边框

cocoa - 处理核心数据模型变更

swift - 手动创建 nsbutton - 调用函数

macos - NSButton 禁用标题颜色始终为灰色