2013-04-29 88 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