Pregunta

I have a custom PDFView subclass and have overridden -mouseDown, -mouseDragged etc to draw a rectangle over the view. Here is the code I am using:

@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

The NSRect is calculated correctly, but when I call [self setNeedsDisplay], -drawRect is never called, and the rectangle is never drawn.

Is there any reason why -drawRect is never called on a PDFView subclass?

¿Fue útil?

Solución

I have a similar use case. According to the docs, override PDFView's drawPage method instead. Continue to call setNeedsDisplay on the PDFView. It works, but it's a little slow. Working on overlaying a view right now instead.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top