cocoa - NSView 不更新?

标签 cocoa drag-and-drop nsview redraw nscolor

我正在研究拖放 View ,并找到了一些用于网络上拖放操作的处理程序。我想让它在用户将文件拖动到拖放区域时变为蓝色,并在退出拖放区域时再次变为灰色。问题是当您将鼠标拖动到它上面或退出它时它不会更新。这是一些代码:

- (void)drawRect:(NSRect)rect
{
   NSRect bounds = [self bounds];
   [[NSColor grayColor] set];
   [NSBezierPath fillRect:bounds];
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender  {
    NSRect bounds = [self bounds];
    [[NSColor blueColor] set];
    [NSBezierPath fillRect:bounds];

    return NSDragOperationCopy;

}

- (void)draggingExited:(id <NSDraggingInfo>)sender {
    NSRect bounds = [self bounds];
    [[NSColor grayColor] set];
    [NSBezierPath fillRect:bounds];

}

感谢您的帮助。

最佳答案

您是否在任何地方调用[yourView: setNeedsDisplay]

这就是让绘图框架知道它需要使用 drawRect: 向您的 UIView 子类发送消息的方式,因此只要情况发生变化,您就应该执行此操作。在您的情况下,这可能意味着鼠标进入或退出放置区域。

关于cocoa - NSView 不更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3243092/

相关文章:

c++ - Qt 拖放 TreeView : what am I missing

c++ - qt在qgraphicsScene中拖拽

ios - 使用自动布局分配中心

objective-c - 在其他 View 上显示 NSView 子类

swift - NSUndoManager:可以捕获引用类型吗?

objective-c - 调用类的成员函数?

macos - 有没有办法遍历 Mac OS X 中所有打开的窗口?

javascript - 拖动时拖放更改图像

objective-c - 如何使命令-A 选择 NSTableView 中行中的所有 NSTextView 文本?

macos - 苹果电脑 : sandbox creation failed: 1002 (operation couldn't be completed 13)