cocoa - 未在 PDFView 子类上调用 drawRect

标签 cocoa subclass nsview drawrect pdfview

我有一个自定义的PDFView子类,并覆盖了-mouseDown-mouseDragged等以在 View 上绘制一个矩形。这是我正在使用的代码:

@implementation MyPDFView {
    NSPoint clickLocation;
    NSRect selection;
}

- (void)mouseDown:(NSEvent *)theEvent {
    NSPoint clickLocationOnWindow = [self.window mouseLocationOutsideOfEventStream];
    clickLocation = [self convertPoint:clickLocationOnWindow fromView:nil];

    NSLog(@"%@", NSStringFromPoint(clickLocation));
}

- (void)mouseDragged:(NSEvent *)theEvent {
    NSPoint mouseLocationOnWindow = [self.window mouseLocationOutsideOfEventStream];
    NSPoint currentLocation = [self convertPoint:mouseLocationOnWindow fromView:nil];

    CGFloat lowerX = fmin(clickLocation.x, currentLocation.x);
    CGFloat lowerY = fmin(clickLocation.y, currentLocation.y);
    CGFloat upperX = fmax(clickLocation.x, currentLocation.x);
    CGFloat upperY = fmax(clickLocation.y, currentLocation.y);

    selection = NSMakeRect(lowerX, lowerY, upperX-lowerX, upperY-lowerY);

    [self setNeedsDisplay:YES];

    NSLog(@"%@", NSStringFromRect(selection));
}

- (void)drawRect:(NSRect)dirtyRect
{
    // Drawing code here.
    NSLog(@"drawRect");

    NSBezierPath *bp = [NSBezierPath bezierPathWithRect:selection];
    [[NSColor blueColor] set];
    [bp fill];
}

@end

NSRect 计算正确,但是当我调用 [self setNeedsDisplay] 时,-drawRect 永远不会被调用,并且矩形也永远不会被调用绘制。

有什么原因导致 PDFView 子类永远不会调用 -drawRect 吗?

最佳答案

我有一个类似的用例。根据文档,请重写 PDFView 的 drawPage 方法。继续在 PDFView 上调用 setNeedsDisplay。它有效,但有点慢。现在正在努力覆盖 View 。

关于cocoa - 未在 PDFView 子类上调用 drawRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15272681/

相关文章:

objective-c - 是否可以检查 UI 元素是否具有特定的引用 socket ?

python - 添加新实例变量的 Python set 类子类化的正确(或最佳)方法是什么?

swift - macOS : CALayer shadow not showing

cocoa - 拖动时未在嵌套 View 上调用 NSView mouseEntered - 黑客攻击是唯一的选择吗?

swift - 访问全局监视器中的触摸事件会引发错误

objective-c - Launchpad 使用哪个控件?

cocoa - 如何调整 NSTextField 的大小以适合它所包含的文本?

带空格和不带空格的 CSS 子类

java - java中类型不匹配?

objective-c - 使用源列表样式向 NSOutlineView 添加自定义 subview