好的,问题在于:
我有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
。
关于如何做到这一点的任何想法?谢谢!
答案 0 :(得分:6)
添加仅跟踪进入/退出事件的跟踪区域似乎不足以支持NSTextView
子视图。不知何故,textview总是获胜并设置为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
答案 1 :(得分:3)
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)
}
}
}