Swift NSView鼠标向下填充矩形

Swift NSView鼠标向下填充矩形,swift,macos,cocoa,nsview,mousedown,Swift,Macos,Cocoa,Nsview,Mousedown,我在菜单栏中为我的按钮创建了一个自定义视图。因为我将其添加为子视图,所以当按钮被点击时,它就不再有效果了,就像这里的电池: 现在我看到我可以覆盖mouseDown效果和mouseUp来手动执行此效果。有人知道我如何添加这种效果吗 编辑:例如,它可以解决我的问题,如果我知道如何在自定义NSView中绘制矩形的背景好的,多亏了这个问题的帮助,我实际上得到了它: override func mouseDown(with theEvent: NSEvent) { switcher = true

我在菜单栏中为我的按钮创建了一个自定义视图。因为我将其添加为子视图,所以当按钮被点击时,它就不再有效果了,就像这里的电池:

现在我看到我可以覆盖mouseDown效果和mouseUp来手动执行此效果。有人知道我如何添加这种效果吗


编辑:例如,它可以解决我的问题,如果我知道如何在自定义NSView中绘制矩形的背景

好的,多亏了这个问题的帮助,我实际上得到了它:

override func mouseDown(with theEvent: NSEvent) {
    switcher = true
    needsDisplay = true
}

override func mouseUp(with theEvent: NSEvent)
{
    switcher = false
    needsDisplay = true
}

override func draw(_ dirtyRect: NSRect) {

    [do the usual thing]

    if (switcher == true)
    {
    // color picked the color of the normal mouseDown event
    let c1  = NSColor(calibratedRed: 48.0/255.0, green: 91.0/255.0, blue: 178.0/255.0, alpha: 1.0)
        c1.setFill()
        self.frame.fill()
        myrect?.fill()
        NSColor.clear.setFill()
        [do the usual thing]
    }
}