可可按钮,用鼠标点亮

时间:2022-09-10 19:54:52

Is there a flag that can be set that will cause a Cocoa button to be highlighted when it is moused over. I need to this programatically with objective C on OSX.

是否有一个可以设置的标志,当它被鼠标悬停时会导致Cocoa按钮突出显示。我需要以编程方式在OSX上使用目标C.

2 个解决方案

#1


15  

Setup a tracking area for the view with addTrackingArea (provided you are using Leopard or newer OS X). You'll get events on mouse enter and mouse exit.

使用addTrackingArea为视图设置跟踪区域(假设您使用的是Leopard或更新的OS X)。您将在鼠标输入和鼠标退出时获得事件。

#2


1  

something below maybe the answer.

以下可能是答案。

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
    }
}

}

}

link: https://github.com/fancymax/HoverButton

链接:https://github.com/fancymax/HoverButton

#1


15  

Setup a tracking area for the view with addTrackingArea (provided you are using Leopard or newer OS X). You'll get events on mouse enter and mouse exit.

使用addTrackingArea为视图设置跟踪区域(假设您使用的是Leopard或更新的OS X)。您将在鼠标输入和鼠标退出时获得事件。

#2


1  

something below maybe the answer.

以下可能是答案。

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
    }
}

}

}

link: https://github.com/fancymax/HoverButton

链接:https://github.com/fancymax/HoverButton