2013-04-29 84 views
3

好了,现在的问题是:
我有一个NSTextView,我添加使用我的自定义NSButton当光标悬停在NSTextView中的NSButton时,如何强制光标变成“arrowCursor”?

[_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

您是否尝试过实施'cursorUpdate:',如[规定”管理游标更新事件“](http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/TrackingAreaObjects/TrackingAreaObjects.html#//apple_ref/doc/uid/10000060i-CH8-SW10) ? – 2013-04-29 20:32:17

+0

是的,但结果是相同的 – 2013-04-29 21:14:37

回答

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 
+0

非常感谢您的答案,但不幸的是它不工作:/ – 2013-04-30 09:01:09

+0

嗨佩德罗, 我刚刚在一个示例Xcode项目中尝试过。像这里的魅力一样(10.8)。你有没有使用我上面发布的子类?如果是这样,你是否实例化它而不是你自己的? – 2013-04-30 09:30:47

+0

哎呀,请原谅我!它现在在工作,而且tbh,我没有改变任何东西:P非常感谢! – 2013-04-30 09:36:13