如何强制光标为“arrowCursor”当它悬停在NSTextView内部的NSButton? [英] How to force the cursor to be an "arrowCursor" when it hovers a NSButton that is inside a NSTextView?

查看:1330
本文介绍了如何强制光标为“arrowCursor”当它悬停在NSTextView内部的NSButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这里是问题:

我有一个 NSTextView ,我添加我的自定义 NSButton 使用:

Okay, here is the problem:
I have a NSTextView and I add my custom NSButton using:

[_textView addSubview:button];

然后,在我的 NSButton (与 NSTrackingArea 材料一起):

Then, inside my NSButton subclass, I have (along with the NSTrackingArea stuff):

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



有关如何做的任何想法?谢谢!

But when I hover it, the cursor remains the same IBeamCursor (because it's a 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.

Any ideas on how to do this? Thank you!

推荐答案

添加仅跟踪进入/退出事件的跟踪区域似乎不足以满足 NSTextView 子视图。不知何故,textview总是赢得并设置 IBeamCursor

Adding a tracking area that only tracks enter/exit events seems to be not enough for NSTextView subviews. Somehow the textview always wins and sets it's IBeamCursor.

您可以尝试始终启用鼠标移动事件( NSTrackingMouseMoved )的跟踪在 NSButton 子类中添加跟踪区域:

You can try to always enable tracking for mouse move events (NSTrackingMouseMoved) when adding the tracking area in your NSButton subclass:

#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

这篇关于如何强制光标为“arrowCursor”当它悬停在NSTextView内部的NSButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆