gpt4 book ai didi

objective-c - 我应该在 NSCell 上调用什么方法

转载 作者:行者123 更新时间:2023-12-03 16:42:00 24 4
gpt4 key购买 nike

我正在使用自定义 NSCell 编写自定义 NSControl。它是一个控件,因此它必须响应鼠标。我在我的控件上创建了一个 NSTrackingArea,实现了 -mouseEntered:-mouseExited:-mouseMoved:。 (我必须实现 -mouseUp/Down:,但我不知道在那里要做什么,所以现在我还没有覆盖这些方法。)在这些方法中,我成功确定鼠标当前位于哪个单元格上。现在我有两个问题:

  • 这是跟踪鼠标的好方法吗?如果没有,我该怎么办?
  • 当鼠标单击、鼠标进入单元格、当鼠标离开单元格等时,我应该在 NSCell 上调用什么方法? Apple 的文档对此不是很清楚。

所以,基本上:我应该什么时候调用 NSCell 上的什么方法来让它响应鼠标事件?

编辑:
重读文档,我想我应该调用 NSCell 的 -trackMouse:inRect:ofView:untilMouseUp: 并覆盖 -startTrackingAt:inView:, -continueTracking:at:inView :-stopTracking:at:inView:mouseIsUp:。同样有两个问题:1)文档给人的印象是这些仅在鼠标按下时才被调用。那是对的吗?那我该怎么办呢? 2) 我应该在哪里/什么时候调用 NSCell 的 -trackMouse:inRect:ofView:untilMouseUp:

最佳答案

我最终实现了自己的鼠标跟踪机制:

// 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;

这些方法的实现为空(因此编译器不会提示,我可以将鼠标处理方法的实现留给子类)。

关于objective-c - 我应该在 NSCell 上调用什么方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15412533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com