Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 鼠标悬停时亮起的可可按钮_Cocoa_Button_Highlighting - Fatal编程技术网

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

Cocoa 鼠标悬停时亮起的可可按钮,cocoa,button,highlighting,Cocoa,Button,Highlighting,是否可以设置一个标志,当鼠标滑过可可按钮时,该标志将使可可按钮高亮显示。我需要在OSX上用objective C编程实现这一点 使用设置视图的跟踪区域(前提是您使用的是Leopard或更新的OS X)。您将在鼠标进入和鼠标退出时获得事件。下面的内容可能就是答案 class HoverButton: NSButton{ var backgroundColor: NSColor? var hoveredBackgroundColor: NSColor? var pressedBackgroundC

是否可以设置一个标志,当鼠标滑过可可按钮时,该标志将使可可按钮高亮显示。我需要在OSX上用objective C编程实现这一点

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

下面的内容可能就是答案

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


链接:

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

如果
仅显示顺序
而emouseinded
为true,则在鼠标指针上调用
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 }
}

您能解释一下如何使用代码(至少用一些示例…)来实现这一点吗?