2013-03-14 7 views
2

Я пишу пользовательский NSControl с пользовательскими NSCells. Это элемент управления, поэтому он должен реагировать на мышь. Я создал NSTrackingArea над моим управлением, реализовано -mouseEntered:, -mouseExited: и -mouseMoved:. (И мне нужно будет реализовать -mouseUp/Down:, но я понятия не имею, что делать там, поэтому пока я еще не переопределил эти методы.) В этих методах я успешно определяю, в какой ячейке находится мышь. Теперь у меня есть два вопроса:Какой метод я должен позвонить в свой NSCell

  • Это хороший подход для отслеживания мыши? Если нет, что мне делать вместо этого?
  • Какой метод я должен вызвать для своего NSCell щелчком мыши, когда мышь входит в ячейку, когда мышь покидает ячейку и т. Д.? Документы Apple не совсем понятны.

Итак, в основном: Когда мне следует называть какой метод на моем NSCell позволить ему реагировать на события мыши?

EDIT:
Перечитывая документы, я думаю, что я должен вызвать NSCell-х -trackMouse:inRect:ofView:untilMouseUp: и переопределить -startTrackingAt:inView:, -continueTracking:at:inView: и -stopTracking:at:inView:mouseIsUp:. Опять два вопроса: 1) документы дают впечатление, что они вызываются только при отключении мыши. Это верно? Тогда что мне делать вместо этого? 2) Где/когда я должен позвонить NSCell's -trackMouse:inRect:ofView:untilMouseUp:?

+1

Взгляните на NSActionCell; он должен дать вам образец, за которым вы хотите следовать. –

+0

Не могли бы вы рассказать об этом? Я хочу добавить подсветку, так что мне нужно больше, чем базовая поддержка цели/действия. @JimPuls – 11684

ответ

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; 

С пустой реализацией для этих методов (так что компилятор не жалуется и я могу оставить реализацию методов обработки мыши подклассами).