2013-03-07 57 views
0

我有一個自定義的PDFView子類,並已覆蓋-mouseDown,-mouseDragged等在視圖上繪製矩形。這裏是我使用的代碼:drawRect未調用PDFView子類

@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子類上調用嗎?

回答

1

我有一個類似的用例。根據文檔,改爲重寫PDFView的drawPage方法。繼續在PDFView上調用setNeedsDisplay。它有效,但有點慢。現在改寫覆蓋視圖。

+0

這正是我需要的幫助。謝謝! – colincameron 2013-10-11 15:48:55