鼠标悬停时会亮起的可可按钮

发布于 2024-08-10 20:14:47 字数 83 浏览 7 评论 0原文

是否有一个可以设置的标志,当鼠标悬停在 Cocoa 按钮上时,该按钮会突出显示。我需要在 OSX 上使用 Objective C 以编程方式实现这一点。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最初的梦 2024-08-17 20:14:47

使用 addTrackingArea(前提是您使用的是 Leopard 或更新的 OS X)。您将收到鼠标进入和鼠标退出的事件。

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.

情魔剑神 2024-08-17 20:14:47

下面的内容也许就是答案。

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

}

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

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

迷爱 2024-08-17 20:14:47

还可以覆盖按钮单元格以将鼠标进入/退出事件传播到视图。我没有测试所有按钮样式,我使用凹进式样式。

如果 showsBorderOnlyWhileMouseInside 为 true,则在 mouseEnter 上调用 drawBezel 函数。
这就是为什么我只是覆盖它,并在按钮中管理我的自定义显示行为。

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

It is also possible to override the button cell to propagate the mouse enter/exit event to the view. I did not test all buttons styles, I use Recessed style.

The drawBezel function is called on mouseEnter if showsBorderOnlyWhileMouseInside is true.
That's why I simply override it, and manage my custom display behaviour in the button.

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 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文