【问题标题】:Custom NSButton. How change color while clicked自定义 NSButton。单击时如何更改颜色
【发布时间】:2016-02-25 18:49:07
【问题描述】:

我想自定义 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 类上执行此功能?

【问题讨论】:

    标签: swift cocoa nsbutton


    【解决方案1】:

    您所要做的就是在特定视图中覆盖drawRect()。 在drawRect可以检查按钮是否高亮。

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

    您无需为此检测mouseDown 及以上。 drawRect 将被自动调用

    【讨论】:

      【解决方案2】:

      我以这种方式实现了自定义按钮颜色突出显示:

          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)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-21
        • 2019-05-20
        • 1970-01-01
        • 1970-01-01
        • 2012-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多