简体   繁体   English

当光标悬停在 NSTextView 内的 NSButton 上时,如何强制光标成为“箭头光标”?

[英]How to force the cursor to be an “arrowCursor” when it hovers a NSButton that is inside a NSTextView?

Okay, here is the problem:好的,问题来了:
I have a NSTextView and I add my custom NSButton using:我有一个NSTextView并使用以下方法添加我的自定义NSButton

[_textView addSubview:button];

Then, inside my NSButton subclass, I have (along with the NSTrackingArea stuff):然后,在我的NSButton子类中,我有(以及NSTrackingArea东西):

- (void)mouseEntered:(NSEvent *)event{
     [[NSCursor arrowCursor] set];
}

- (void)mouseExited:(NSEvent *)theEvent{
     [[NSCursor arrowCursor] set];
}

- (void)mouseDown:(NSEvent *)theEvent{
     [[NSCursor arrowCursor] set];
}

- (void)mouseUp:(NSEvent *)theEvent{
     [[NSCursor arrowCursor] set];
}

But when I hover it, the cursor remains the same IBeamCursor (because it's a NSTextView ).但是当我悬停它时,光标仍然是同一个IBeamCursor (因为它是一个NSTextView )。 Only when I press the button, the cursor gets updated.只有当我按下按钮时,光标才会更新。 And then, when I move the mouse, still inside the button, the cursor goes back to the IBeamCursor .然后,当我移动鼠标时,仍然在按钮内,光标回到IBeamCursor

Any ideas on how to do this?关于如何做到这一点的任何想法? Thank you!谢谢!

Adding a tracking area that only tracks enter/exit events seems to be not enough for NSTextView subviews.添加仅跟踪进入/退出事件的跟踪区域对于NSTextView子视图似乎是不够的。 Somehow the textview always wins and sets it's IBeamCursor .不知何故, textview 总是获胜并将其设置为IBeamCursor

You can try to always enable tracking for mouse move events ( NSTrackingMouseMoved ) when adding the tracking area in your NSButton subclass:NSButton子类中添加跟踪区域时,您可以尝试始终启用对鼠标移动事件 ( NSTrackingMouseMoved ) 的跟踪:

#import "SSWHoverButton.h"

@interface SSWHoverButton()
{
    NSTrackingArea* trackingArea;
}

@end

@implementation SSWHoverButton

- (void)mouseMoved:(NSEvent*)theEvent
{
    [[NSCursor arrowCursor] set];
}

- (void)updateTrackingAreas
{
    if(trackingArea != nil)
    {
        [self removeTrackingArea:trackingArea];
    }
    NSTrackingAreaOptions opts = (NSTrackingMouseMoved|NSTrackingActiveAlways);
    trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds]
                                                 options:opts
                                                   owner:self
                                                userInfo:nil];
    [self addTrackingArea:trackingArea];
}

- (void)dealloc
{
    [self removeTrackingArea:trackingArea];
}

@end

Swift 5 variant: Swift 5 变体:

import Cocoa

class InsideTextButton: NSButton {

    var trackingArea: NSTrackingArea?

    override func mouseMoved(with event: NSEvent) {
        NSCursor.arrow.set()
    }

    override func updateTrackingAreas() {
        if let area = trackingArea {
            removeTrackingArea(area)
        }

        trackingArea = NSTrackingArea(rect: self.bounds, options: [.mouseMoved, .activeAlways], owner: self, userInfo: nil)

        if let area = trackingArea {
            addTrackingArea(area)
        }
    }

    deinit {
        if let area = trackingArea {
            removeTrackingArea(area)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM