使用Tracking-Area對象


創建NSTrackingArea對象 

 一個NSTrackingArea對象定義一個鼠標的運動敏感區域,當鼠標進入(Enter)/移動(moves)/離開(exits)這個敏感區域,Application Kit 發送 mouse-tracking, mouse-moved, and cursor-update 消息。

當你創建一個nstrackingarea對象必須指定一個或多個選項。這些參數是配置跟蹤區行為。一般分為三類:

1.消息類型

如果你要求處理mouseEntered: 和mouseExited:消息,你需要添加NSTrackingMouseEnteredAndExited參數

如果你要求處理 mouseMoved:消息,你需要添加NSTrackingMouseMoved參數

如果你要求處理 cursorUpdate:消息,你需要添加NSTrackingCursorUpdate參數

2.追蹤鼠標活動范圍(必須設置下面一種)

NSTrackingActiveWhenFirstResponder 跟蹤第一響應者View的消息

NSTrackingActiveInKeyWindow 跟蹤關鍵窗口中View的消息

NSTrackingActiveInActiveApp 跟蹤活動App中View的消息

NSTrackingActiveAlways 跟蹤活動App任何時候的消息

3.跟蹤區行為的高級設置

NSTrackingAssumeInside: 如果你想跟蹤處理鼠標首次離開tracking area的消息

NSTrackingInVisibleRect:你可以跟蹤和關聯View的可見區域相同的一個區域的消息

NSTrackingEnabledDuringMouseDrag:你可以要求用鼠標拖動進出跟蹤區域產生mouseentered:mouseexited:事件

 

通過NSTrackingArea initWithRect:options:owner:userInfo:創建一個實例對象。創建對象后,你必須通過NSView addTrackingArea:方法關聯一個View對象。list6-1在一個自定義View的實例initwithframe:方法中創建和添加一個NSTrackingArea對象;在這種情況下,每當窗口是key window時,這個自定義View接收Application Kit發送來的mouseEntered:mouseExited: mouseMoved:消息

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        trackingArea = [[NSTrackingArea alloc] initWithRect:eyeBox
            options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow )
            owner:self userInfo:nil];
        [self addTrackingArea:trackingArea];
    }
    return self;
}

 

管理跟蹤對象updateTrackingAreas

1.當View添加到Window或者從window中刪除,或者改變View在window中的位置,Application kit會重新計算Tracking-Area區域。

2. 當NSTrackingArea相關聯的View不在可見區域時,Application kit無法重新計算Tracking-Area區域,除非NSTrackingArea在創建時設置NSTrackingInVisibleRect屬性。

3. 如果不是自定義的View,不能在初始化中創建NSTrackArea對象,可以注冊NSViewFrameDidChangeNotification消息,在消息處理函數中重創建NSTrackArea對象接收消息

- (void)updateTrackingAreas {
    NSRect eyeBox;
    [self removeTrackingArea:trackingArea];
    [trackingArea release];
    eyeBox = [self resetEye];
    trackingArea = [[NSTrackingArea alloc] initWithRect:eyeBox
        options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow)
        owner:self userInfo:nil];
    [self addTrackingArea:trackingArea];
}

 

Managing Cursor-Update Events

1.一般情況下,光標進入不同類型的視圖中,會變化成不同的光標圖像。比如本文視圖需要一個I型光標。很多Application kit類中都定義自己獨特的光標圖像,您可以為自己的自定義視圖的子類實例的指定一個不同的光標圖像

2.你想在自定義NSResponder(NSView)的cursorUpdate:方法中改變光標圖像,你必須在創建NSTrackingArea對象方法initWithRect:options:owner:userInfo:中添加NSTrackingCursorUpdate屬性,然后把NSTrackingArea對象使用addTrackingArea:方法關聯View對象。此后,此后,該鼠標在進入跟蹤區域后,會產生一個nscursorupdate的事件;NSWindow對象會發送cursorupdate:事件到跟蹤區域的所有者(關聯的NSView對象),在NSView的cursorUpdate:方法中改變光標圖像。

3. nscursorupdate的事件會在響應鏈中傳遞

4.注意:當鼠標退出跟蹤區域時,沒有必要設置光標圖像使它恢復原樣。因為 Application Kit自動幫你處理。

-(void)cursorUpdate:(NSEvent *)theEvent
{
    [[NSCursor crosshairCursor] set];
}

 

 相關學習

老譚的<OSX的MouseEntered和MouseExited事件檢測>http://www.tanhao.me/pieces/1808.html/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM