マウスオーバーしたときにCocoaボタンが強調表示されるように設定できるフラグはありますか。OSXのObjectiveCを使用してプログラムでこれを行う必要があります。
3583 次
3 に答える
16
addTrackingAreaを使用してビューの追跡領域を設定します(Leopard以降のOS Xを使用している場合)。マウスの入力と終了時にイベントが発生します。
于 2009-11-09T23:53:05.630 に答える
1
以下の何かが答えかもしれません。
class HoverButton: NSButton{
var backgroundColor: NSColor?
var hoveredBackgroundColor: NSColor?
var pressedBackgroundColor: NSColor?
private var hovered: Bool = false
override var wantsUpdateLayer:Bool{
return true
}
required init?(coder: NSCoder) {
super.init(coder: coder)
self.commonInit()
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.commonInit()
}
func commonInit(){
self.wantsLayer = true
self.createTrackingArea()
self.hovered = false
self.hoveredBackgroundColor = NSColor.selectedTextBackgroundColor()
self.pressedBackgroundColor = NSColor.selectedTextBackgroundColor()
self.backgroundColor = NSColor.clearColor()
}
private var trackingArea: NSTrackingArea!
func createTrackingArea(){
if(self.trackingArea != nil){
self.removeTrackingArea(self.trackingArea!)
}
let circleRect = self.bounds
let flag = NSTrackingAreaOptions.MouseEnteredAndExited.rawValue + NSTrackingAreaOptions.ActiveInActiveApp.rawValue
self.trackingArea = NSTrackingArea(rect: circleRect, options: NSTrackingAreaOptions(rawValue: flag), owner: self, userInfo: nil)
self.addTrackingArea(self.trackingArea)
}
override func mouseEntered(theEvent: NSEvent) {
self.hovered = true
NSCursor.pointingHandCursor().set()
self.needsDisplay = true
}
override func mouseExited(theEvent: NSEvent) {
self.hovered = false
NSCursor.arrowCursor().set()
self.needsDisplay = true
}
override func updateLayer() {
if(hovered){
if (self.cell!.highlighted){
self.layer?.backgroundColor = pressedBackgroundColor?.CGColor
}
else{
self.layer?.backgroundColor = hoveredBackgroundColor?.CGColor
}
}
else{
self.layer?.backgroundColor = backgroundColor?.CGColor
}
}
}
于 2016-01-23T08:39:58.067 に答える
0
ボタンセルをオーバーライドして、マウスのEnter/Exitイベントをビューに伝達することもできます。すべてのボタンスタイルをテストしたわけではなく、凹型スタイルを使用しています。
trueのdrawBezel
場合、関数はmouseEnterで呼び出されshowsBorderOnlyWhileMouseInside
ます。そのため、単純にオーバーライドして、ボタンでカスタム表示の動作を管理します。
class myButtonCell: NSButtonCell {
override func mouseEntered(with event: NSEvent) {
controlView?.mouseEntered(with: event)
// Comment this to remove title highlight (for button style)
super.mouseEntered(with: event)
}
override func mouseExited(with event: NSEvent) {
controlView?.mouseExited(with: event)
// Comment this to remove title un-highlight (for recessed button style)
// Here, we un-hilight the title only if the button is not selected
if state != .on { super.mouseExited(with: event) }
}
// removes the default highlight behavior
override func drawBezel(withFrame frame: NSRect, in controlView: NSView) {}
}
// Set the cell class to MyButtonCell in interface builder
class MyButton: NSButton {
var mouseIn: Bool = false { didSet {
// Up to you here - setNeedsDisplay() or layer update
}}
override func mouseEntered(with event: NSEvent) { mouseIn = true }
override func mouseExited(with event: NSEvent) { mouseIn = false }
}
于 2020-11-20T11:50:17.833 に答える