2013-03-14 55 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; 

随着这些方法的空实现(所以编译器不抱怨我可以将鼠标处理方法的实现留给子类)。