首页 > 解决方案 > NSView mouseDown 填充矩形

问题描述

我在菜单栏中为我的按钮制作了一个自定义视图。因为我将它添加为子视图,所以当按钮被点击时,它消除了效果,就像这里的电池一样:

在此处输入图像描述

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

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

标签: swiftmacoscocoansviewmousedown

解决方案


好的,由于这个问题的帮助,我实际上得到了它:Best way to change the background color for an 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]
    }
}

推荐阅读