【问题标题】:drawRect not called on PDFView subclass未在 PDFView 子类上调用 drawRect
【发布时间】:2013-03-07 13:39:03
【问题描述】:

我有一个自定义的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 子类上调用过吗?

【问题讨论】:

    标签: cocoa subclass nsview drawrect pdfview


    【解决方案1】:

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

    【讨论】:

    • 这正是我需要的帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多