我有一个来自NSButtonCell
的类,我绘制了表圈:
override func drawBezel(withFrame frame: NSRect, in controlView: NSView) {
let path = NSBezierPath(bound: frame.insetBy(dx: CGFloat(config.buttonInset), dy: CGFloat(config.buttonInset)), withCorners: corners, withRadius: CGFloat(config.cornerRadius), flip: flipIt)
path.lineWidth = config.borderWidth
if(isEnabled)
{
if(isHighlighted)
{
print("isHighlighted true")
let fillColor: NSColor = colorMap.buttonHighlightColor
let strokeColor: NSColor = colorMap.buttonBorderColor
fillColor.setFill()
strokeColor.setStroke()
path.fill()
path.stroke()
}
else
{
print("isHighlighted false")
if(showsStateBy.contains(.changeGrayCellMask))
{
print(".changeGrayCellMask")
if(state == .on)
{
print(".on")
let fillColor: NSColor = colorMap.buttonOnColor
let strokeColor: NSColor = colorMap.buttonBorderColor
fillColor.setFill()
strokeColor.setStroke()
path.fill()
path.stroke()
}
else
{
print(".off")
let fillColor: NSColor = colorMap.buttonBackgroundColor
let strokeColor: NSColor = colorMap.buttonBorderColor
fillColor.setFill()
strokeColor.setStroke()
path.fill()
path.stroke()
}
}
else
{
print("!.changeGrayCellMask")
let fillColor: NSColor = colorMap.buttonBackgroundColor
let strokeColor: NSColor = colorMap.buttonBorderColor
fillColor.setFill()
strokeColor.setStroke()
path.fill()
path.stroke()
}
}
}
else
{
let fillColor: NSColor = colorMap.buttonBackgroundDisabledColor
let strokeColor: NSColor = colorMap.buttonBorderColor
fillColor.setFill()
strokeColor.setStroke()
path.fill()
path.stroke()
}
}
另外,我使用我的自定义单元格将keyEquivalent
分配给按钮。
无论是在macOS High Sierra上使用鼠标点击还是按键,这都可以完美地运行。仅在鼠标或键关闭时才会显示突出显示。
日志输出如下所示:
**after click with mouse**
isHighlighted true
isHighlighted false
!.changeGrayCellMask
**after shortcut key**
isHighlighted true
isHighlighted false
!.changeGrayCellMask
但是,在Mojave上,按键的行为是不同的。按下按键后,突出显示的状态仍然存在,而在使用鼠标时,突出显示按预期运行。
Mojave的日志输出:
**Mojave click with mouse**
isHighlighted true
isHighlighted false
!.changeGrayCellMask
**Mojave after shortcut key**
isHighlighted false
!.changeGrayCellMask
isHighlighted true <----- this is odd
莫哈韦也有变化的东西。正如你所看到的,drawBezel
通话令是完全出乎意料的。奇怪的是它只在使用键盘时才会发生。
如何使用键盘类似于鼠标点击Mojave来实现按钮突出显示行为?
UPDATE
我能够在XCode Playground中创建最小的项目来演示这个问题。你可以下载它here
内部动作:
[button display];
这可能是一个非常优雅的解决方案。但它对我有用。