自定义NSButton。如何在点击时更改颜色

时间:2016-02-25 18:49:08

标签: swift cocoa nsbutton

我想自定义myButton以便在mouseDown发生时更改背景颜色,并在调用mouseUp时返回默认颜色。

override func mouseDown(theEvent: NSEvent) {
    super.mouseDown(theEvent)
    self.bgColor = NSColor(hex: 0x4A7AA1)
    self.textColor = NSColor.darkGrayColor()
    self.needsDisplay = true
    self.mouseUp(theEvent)
}

override func mouseUp(theEvent: NSEvent) {
    self.textColor = NSColor.whiteColor()
    self.bgColor = NSColor(hex: 0x6AAFE6, alpha: 0.95)
}

我试图运行此代码,但鼠标停止时所有视图都不重绘。如何在myButton上执行此功能:NSButton类?

2 个答案:

答案 0 :(得分:0)

您只需在特定视图中覆盖drawRect()即可。 在drawRect中,您可以检查按钮是否突出显示。

func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)
    if self.highlighted {
         // Do custom drawing.
    }
}

您不需要检测mouseDown以及此问题。 drawRect将自动调用

答案 1 :(得分:0)

我已经通过这种方式实现了自定义按钮颜色的高亮显示:

    override func draw(_ dirtyRect: NSRect) {
    super.draw(dirtyRect);

    setHighlightedColors();
}

private func setHighlightedColors() {
    let textColorDifference: CGFloat = 50/255;

    let textHighlightedColor = textColor == .clear ? textColor : NSColor(red: textColor.redComponent - textColorDifference,
                                                                      green: textColor.greenComponent - textColorDifference,
                                                                      blue: textColor.blueComponent - textColorDifference,
                                                                      alpha: textColor.alphaComponent);


    let backgroundColorDifference: CGFloat = 20/255;
    let backgroundHighlightedColor = backgroundColor == .clear ? backgroundColor : NSColor(red: backgroundColor.redComponent - backgroundColorDifference,
                                                                                  green: backgroundColor.greenComponent - backgroundColorDifference,
                                                                                  blue: backgroundColor.blueComponent - backgroundColorDifference,
                                                                                  alpha: backgroundColor.alphaComponent);

    let borderColorDifference: CGFloat = 50/255;
    let borderHighlightedColor = borderColor == .clear ? borderColor : NSColor(red: borderColor.redComponent - borderColorDifference,
                                                                          green: borderColor.greenComponent - borderColorDifference,
                                                                          blue: borderColor.blueComponent - borderColorDifference,
                                                                          alpha: borderColor.alphaComponent);
    if isHighlighted {
        applyTextColorAndFont(color: textColor, font: customFont);
        applyBackgroundColor(color: backgroundHighlightedColor);
        applyBorder(color: borderHighlightedColor, width: borderWidth)
    } else {
        applyTextColorAndFont(color: textHighlightedColor, font: customFont);
        applyBackgroundColor(color: backgroundColor);
        applyBorder(color: borderColor, width: borderWidth)
    }
}