- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用自定义 NSCell 编写自定义 NSControl。它是一个控件,因此它必须响应鼠标。我在我的控件上创建了一个 NSTrackingArea,实现了 -mouseEntered:
、-mouseExited:
和 -mouseMoved:
。 (我必须实现 -mouseUp/Down:
,但我不知道在那里要做什么,所以现在我还没有覆盖这些方法。)在这些方法中,我成功确定鼠标当前位于哪个单元格上。现在我有两个问题:
所以,基本上:我应该什么时候调用 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/
我正在尝试子类化 NSCell 来绘制自定义背景突出显示。该文档似乎建议重写 highlight:withFrame:inView: 应该允许我执行此操作,但该方法永远不会被调用。 相反,我重写了 d
我的 NSTableView 有可变高度的行。完成单元格编辑后以及调整列/表大小期间/之后,行会正确调整大小。当用户在文本单元格中键入内容时,行也会正确调整大小。 但是,在编辑期间单元格本身不会调整大
我正在实现 NSActionCell 的子类(在 NSTableView 内),并注意到一些不寻常的事情。如果我在用户单击单元格时设置属性 (isEditing),则该属性的值会丢失,因为 NSCel
我正在尝试为 NSOutlineView 中的某些单元格设置动画。我需要制作一个图标动画,以作为左侧面板上设备旁边的 iTunes 同步图标进行旋转。 我在动画领域并没有真正的经验,我通过浏览互联网找
过去几天我一直在阅读和尝试在自定义绘制的 NSCell 中允许链接,但基本上没有任何可用的东西,每种方法总是存在问题。 有人知道有效的方法吗? 我使用 - (void)drawInteriorWith
我正在创建一个 NSCell 子类,它将一些对象直接绘制到 View 上(使用 drawInRect:fromRect:operation:fraction:respectFlipped:hints:
我有一个 NSTableView,当用户滚动 tableView 时,我想跟踪包含它的 NSCell 的位置。 我找不到任何有用的东西。如果有人能引导我走向正确的方向,那就太好了! 编辑: 感谢 @K
无法弄清楚如何处理鼠标在NSCell(或NSTextAttachmentCell)上的移动。这可能吗? 最佳答案 您可以在包含单元格的 View 中添加跟踪区域,并在该 View 中实现 mouseE
当我在 NSTableView 中选择 NSTextFieldCell 时,行为会根据是否选择行而有所不同。 如果选择了该行,则立即选择单元格,并且光标在文本字段内闪烁 如果未选择该行,则单击后唯一的
有没有办法在给定表格中选定单元格的行和列索引的情况下获取 NSCell 对象? 我无法使用 NSTableView 委托(delegate)方法,例如 tableView:willDisplayCel
我有一个 NSCell 子类,我可以在其中进行各种自定义绘图。唯一一次事情似乎完全超出我的控制范围是右键单击单元格以显示关联的上下文菜单时。在显示它的上下文菜单时,表格(或单元格 - 不确定到底是哪个
为什么NSImage在NSTableView的NSCell中绘制翻转。以及如何正确纠正这种行为。我从数据源方法返回图像: - (id)tableView:(NSTableView *)tableVie
我希望 OutlineView 中的 NSCell 显示其所在行的子级数。这就是我的意思: (来源:sourceforge.net) 右边是什么,但适用于所有行。 我将如何首先找出 NSCell 所在
我正在使用自定义 NSCell 编写自定义 NSControl。它是一个控件,因此它必须响应鼠标。我在我的控件上创建了一个 NSTrackingArea,实现了 -mouseEntered:、-mou
我正在使用 NSAttributedString 格式化的数据填充 NSOutlineView。到目前为止,我已经设置了文本字体、大小和颜色的格式。我的问题是,选择行时前景色不会改变。如果您创建一个
在我的 cocoa 应用程序中,我需要一个用于 NSTableView 的自定义 NSCell。这个 NSCell 子类包含一个自定义 NSButtonCell 用于处理点击(以及两个或三个用于文本内
我有一个自定义的 NSCell,其中包含各种元素(图像、各种文本片段),其中一个文本 block 可能包含各种可点击的链接。我有我的 NSAttributedString 正确识别链接并将它们着色为蓝
本质上,这是我在 NSTextFieldCell 子类中的代码: - (void)setStringValue:(NSString *)aString { [super setStringVa
我有一个自定义 NSObjects 的 NSArray。每个对象都有一些属性和一个我想在 GridView 中显示的图像。 NSMatrix 似乎是解决我的问题的一个很好的解决方案,但我在获取要显示的
我有一个 NSTableView,其中包含我的自定义 NSCell 子类 IconCell。 IconCell 包含三个元素:图像、文本和按钮。 这是我的绘图代码的简化版本(closeButton 是
我是一名优秀的程序员,十分优秀!