Swift 如何为NSButtonCell指定alpha?(适用于残疾状态)

Swift 如何为NSButtonCell指定alpha?(适用于残疾状态),swift,cocoa,Swift,Cocoa,我正在覆盖drawBezel以绘制具有特定形状的按钮。现在我想要的是,当它被禁用时,将单元格的alpha设置为50%。我知道我必须使用isEnabled,但是如何为整个单元格指定alpha,包括标题 class FunkyCell: NSButtonCell { ... override func drawTitle(_ title: NSAttributedString, withFrame frame: NSRect, in controlView: NSView) ->

我正在覆盖
drawBezel
以绘制具有特定形状的按钮。现在我想要的是,当它被禁用时,将单元格的alpha设置为50%。我知道我必须使用
isEnabled
,但是如何为整个单元格指定alpha,包括标题

class FunkyCell: NSButtonCell
{
    ...
    override func drawTitle(_ title: NSAttributedString, withFrame frame: NSRect, in controlView: NSView) -> NSRect {
        return super.drawTitle(title, withFrame: frame.offsetBy(dx: 0, dy: 0), in: controlView)
    }

    func redraw() {
        self.backgroundColor = self.backgroundColor
    }

    override func drawBezel(withFrame frame: NSRect, in controlView: NSView) {
        let path = NSBezierPath(bound: frame.insetBy(dx: 2, dy: 2), withCorners: corners, withRadius: CGFloat(NumKeypadConstants.cornerRadius), flip: flipIt)
        path.lineWidth = NumKeypadConstants.borderWidth
        if(isHighlighted)
        {
            let fillColor: NSColor = NumKeypadConstants.buttonHighlightColor
            let strokeColor: NSColor = NumKeypadConstants.buttonBorderColor
            fillColor.setFill()
            strokeColor.setStroke()
            path.fill()
            path.stroke()
        }
        else
        {
            let fillColor: NSColor = NumKeypadConstants.buttonBackgroundColor
            let strokeColor: NSColor = NumKeypadConstants.buttonBorderColor
            fillColor.setFill()
            strokeColor.setStroke()
            path.fill()
            path.stroke()
        }
    }
}
使用:


参考:

我使用的是NSButton,它没有alpha成员。@Pablo我更新了代码,在macOS上必须使用.alphaValue而不是.alpha。如果我在它接收的controlView上的
drawBezel
中使用它,那么程序会在没有任何痕迹的情况下崩溃(在操场上)。你为什么要在drawBezel中这样做?您不应该在事件处理程序中执行此操作吗?实际上改变按钮状态的那个?
let button = self.controlView as? NSButton
button?.alphaValue = 0.5