cocoa objective-c nsbutton nscursor nstrackingarea

objective c - 当鼠标悬停在NSTextView内的NSButton时,如何强制将光标变为“ arrowCursor”

发布于 2020-04-07 10:12:40

好的,这是问题所在:
我有一个,NSTextView然后NSButton使用以下命令添加了自定义

[_textView addSubview:button];

然后,在我的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];
}

但是当我将其悬停时,光标保持不变IBeamCursor(因为它是NSTextView)。只有当我按下按钮时,光标才会更新。然后,当我移动鼠标时,仍在按钮内,光标返回到IBeamCursor

有关如何执行此操作的任何想法?谢谢!

查看更多

提问者
Pedro Vieira
被浏览
176
Thomas Zoechling 2013-04-30 14:35

添加仅跟踪进入/退出事件的跟踪区域似乎不足以实现NSTextView子视图。文本视图总是以某种方式赢得并设置它 IBeamCursor

子类中添加跟踪区域时,您可以尝试始终启用对鼠标移动事件NSTrackingMouseMoved的跟踪NSButton

#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