我正在研究拖放 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/