0

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

@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永远不会调用,并且永远不会绘制矩形。

有什么理由-drawRect永远不会在 PDFView 子类上调用?

4

1 回答 1

1

我有一个类似的用例。根据文档,改写 PDFView 的 drawPage 方法。继续在 PDFView 上调用 setNeedsDisplay。它有效,但它有点慢。现在改为覆盖视图。

于 2013-10-07T15:55:24.190 回答