2013-03-14 57 views
2

我正在使用自定義NSCell編寫自定義NSControl。它是一個控件,所以它必須響應鼠標。我創建了一個NSTrackingArea覆蓋我的控件,實現了-mouseEntered:-mouseExited:-mouseMoved:。 (我將不得不實施-mouseUp/Down:,但我不知道該做什麼,所以現在我還沒有重寫這些方法。)在這些方法中,我成功地確定了鼠標當前在哪個單元格上。現在我有兩個問題:我應該在我的NSCell上調用什麼方法

  • 這是跟蹤鼠標的好方法嗎?如果不是,我該怎麼做呢?
  • 在鼠標點擊,鼠標進入細胞,鼠標離開細胞等時,我應該在我的NSCell上調用什麼方法?蘋果的文檔對此並不十分清楚。

因此,基本上:什麼時候應該調用我的NSCell上的什麼方法讓它響應鼠標事件?

編輯:
李自成的文檔,我想我應該叫的NSCell的-trackMouse:inRect:ofView:untilMouseUp:並覆蓋-startTrackingAt:inView:-continueTracking:at:inView:-stopTracking:at:inView:mouseIsUp:。再次提出兩個問題:1)文檔給人留下的印象只有在鼠標關閉時才被調用。那是對的嗎?那我應該怎麼做呢? 2)我應該在哪裏/何時致電NSCell的-trackMouse:inRect:ofView:untilMouseUp:

+1

看看NSActionCell;它應該給你你想要遵循的模式。 – 2013-03-14 15:19:57

+0

你可以擴展一下嗎?我想添加突出顯示,所以我需要的不僅僅是基本的目標/行動支持。 @JimPuls – 11684 2013-03-14 15:23:38

回答

1

我最終實現我自己的鼠標跟蹤機制:

// MyControl.m: 

- (void)mouseDown:(NSEvent *)theEvent { 
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]]; 
    if (currentCellIndex < [cells count]) { 
     MKListCell *cell = [cells objectAtIndex:currentCellIndex]; 
     currentCell = cell; 
     [currentCell mouseDown:theEvent]; 
    } 
} 

- (void)mouseUp:(NSEvent *)theEvent { 
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]]; 
    if (currentCellIndex < [cells count]) { 
     MKListCell *cell = [cells objectAtIndex:currentCellIndex]; 
     [cell mouseUp:theEvent]; 
    } 
} 

- (void)mouseEntered:(NSEvent *)theEvent { 
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]]; 
    if (currentCellIndex < [cells count]) { 
     MKListCell *cell = [cells objectAtIndex:currentCellIndex]; 
     currentCell = cell; 
     [currentCell mouseEntered:theEvent]; 
    } 
} 

- (void)mouseExited:(NSEvent *)theEvent { 
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]]; 
    if (currentCellIndex < [cells count]) { 
     MKListCell *cell = [cells objectAtIndex:currentCellIndex]; 
     [cell mouseExited:theEvent]; 
     currentCell = nil; 
    } 
} 

- (void)mouseMoved:(NSEvent *)theEvent { 
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]]; 
    MKListCell *cell; 
    if (currentCellIndex < [cells count]) { 
     cell = [cells objectAtIndex:currentCellIndex]; 
    } 
    if (currentCell != cell) { 
     [currentCell mouseExited:theEvent]; 
     [cell mouseEntered:theEvent]; 
     currentCell = cell; 
    } 
    else { 
     [currentCell mouseMoved:theEvent]; 
    } 
} 

- (void)mouseDragged:(NSEvent *)theEvent { 
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]]; 
    MKListCell *cell = nil; 
    if (currentCellIndex < [cells count]) { 
     cell = [cells objectAtIndex:currentCellIndex]; 
    } 
    if (currentCell != cell) { 
     [currentCell mouseExited:theEvent]; 
     [cell mouseEntered:theEvent]; 
     currentCell = cell; 
    } 
    else { 
     [currentCell mouseMoved:theEvent]; 
    } 
} 

- (int)indexOfCellAtPoint:(NSPoint)p { 
    int cellIndex = (self.bounds.size.height - p.y)/cellHeight; 
    return cellIndex; 
} 

當然,在MyCell.h

- (void)mouseDown:(NSEvent *)event; 
- (void)mouseUp:(NSEvent *)event; 
- (void)mouseMoved:(NSEvent *)event; 
- (void)mouseEntered:(NSEvent *)event; 
- (void)mouseExited:(NSEvent *)event; 

隨着這些方法的空實現(所以編譯器不抱怨我可以將鼠標處理方法的實現留給子類)。