【问题标题】:Cocoa Button that will light up with mouse over鼠标悬停时会亮起的 Cocoa 按钮
【发布时间】:2010-12-14 20:28:20
【问题描述】:

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

【问题讨论】:

    标签: cocoa button highlighting


    【解决方案1】:

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

    【讨论】:

    • 你能解释一下如何用代码做到这一点(至少有一些例子......)?
    【解决方案2】:

    下面的东西也许是答案。

    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

    【讨论】:

      【解决方案3】:

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

      如果showsBorderOnlyWhileMouseInside 为真,则在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 }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-12
        • 1970-01-01
        • 2011-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多