我使用[PDFView setNeedsDisplay:YES]让PDF视图重绘,它在OSX 10.9-10.11上运行良好.
但是,除非我放大或缩小PDF页面,否则它不起作用…
但是,除非我放大或缩小PDF页面,否则它不起作用…
NSRect newBounds; NSRect currentBounds; NSRect dirtyRect; NSPoint mouseLoc; NSPoint endPt; // Where is annotation now? currentBounds = [_activeAnnotation bounds]; // Mouse in display view coordinates. mouseLoc = [self convertPoint: [theEvent locationInWindow] fromView: NULL]; // Convert end point to page space. if(activePage == nil) activePage =[_activeAnnotation page]; _LinePoint= [self convertPoint: mouseLoc toPage: activePage]; endPt = [self convertPoint: mouseLoc toPage: activePage]; if(_selectedIdx == 3) //ink { [(PDFAnnotationInk*)_activeAnnotation removeBezierPath:_path]; //endPt.x=_xPoint.x; //竖线 //endPt.y=_xPoint.y; //横线 [_path lineToPoint:endPt]; // 普通笔 [(PDFAnnotationInk*)_activeAnnotation addBezierPath:_path]; [self annotationChanged]; [self setNeedsDisplay:YES]; return;
更新:
我发现setNeedsDispaly调用drawPage:toContext:但是绘图代码在drawPage:toContext中不起作用:
- (void)drawPage:(PDFPage *)pdfPage toContext(CGContextRef)context { [super drawPage: pdfPage toContext:context]; NSBezierPath *line=[NSBezierPath bezierPath]; [line moveToPoint:_xPoint]; [line lineToPoint:NSMakePoint(150,150)]; [[NSColor redColor] set]; [line setLineWidth:50] ; [line stroke]; }
调试说CGContextSetFillColorWithColor:无效的上下文0x0和更多无效的上下文0x0警告.
我在drawPage中做了什么:toContext:正在测试并且只是使用BezierPath来绘制一条线.
解决方法
我遇到了同样的麻烦.我第一次添加注释时,PDFView会立即在页面上显示该注释.从那时起,添加或删除注释在代码中工作正常但PDFView在我手动滚动视图之前不会显示更改.
从PDFKit我尝试过:
previewView.layoutDocumentView() for pageIndex in 0...pdf.pageCount - 1 { let page = pdf.page(at: pageIndex)! previewView.annotationsChanged(on: page) }
从NSView我尝试过:
previewView.needsDisplay = true previewView.needsLayout = true previewView.documentView?.needsDisplay = true previewView.updateLayer()