【发布时间】:2018-05-31 21:43:04
【问题描述】:
我为菜单栏中的按钮创建了一个自定义视图。由于我将其添加为子视图,因此它消除了单击按钮时的效果,就像这里的电池一样:
现在我看到我可以覆盖 mouseDown 效果和 mouseUp 来手动执行此效果。有谁知道我如何添加这种效果?
编辑:例如,如果我知道如何在我的自定义 NSView 中绘制矩形的背景,它将解决我的问题
【问题讨论】:
标签: swift macos cocoa nsview mousedown
我为菜单栏中的按钮创建了一个自定义视图。由于我将其添加为子视图,因此它消除了单击按钮时的效果,就像这里的电池一样:
现在我看到我可以覆盖 mouseDown 效果和 mouseUp 来手动执行此效果。有谁知道我如何添加这种效果?
编辑:例如,如果我知道如何在我的自定义 NSView 中绘制矩形的背景,它将解决我的问题
【问题讨论】:
标签: swift macos cocoa nsview mousedown
好的,感谢这个问题的帮助,我实际上得到了它: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]
}
}
【讨论】: